How to assign ClientClick in the code behind

Dec 19, 2011 at 8:34 AM


I'm trying to assign a function to an OfficeButton via the code behind, but the ClientClick property doesn't accept the new string. If I assign the function in the mark up part it works.

Unfortunately I have to assign the function via the code behind because I need dynamic information.

I have tried to assing the funcion in diefferent events (OnLoad, Page_Load...) nothing works.

Thanks for your help.

Dec 19, 2011 at 11:56 AM

Are you creating an event handler for the function?  it is not assigned as a normal string like in markup.

Check out ContosoWebApp in the demo project, CustomerDetails.aspx for an example.


Dec 19, 2011 at 1:46 PM


What I want to do is to assign a new javascript function to the ClientClick property.
I have a method in the code behind that changes the ClientClick string like this:

        protected void AssignLocalClientScripts()
            String helpUrl = this.ResourceManager.GetString("HELP_URL");
            this.btnHelp.ClientClick =  "BackStage_ItemClick(e, '" + helpUrl + "', '" + options.REDIRECTSURL +
                "', 'modalpages/documentdownload.aspx', '" + info.REDIRECTSURL + "'); return false;";

When I assign the string right in the mark up part it works just fine.
When I call this method in the OnLoad or Page_Load the ClientClick property gets the new value as long as it is in the method (check it using a break point) but loses it somewhere afterwards.

If I debug in the browser the property has the default value (___do Postback...) again.

I guess I have to assign the string on some later point, but I don't know when.

Any idea?

Dec 19, 2011 at 2:30 PM

hey sorry, misread your post earlier.

Did you try on_init?

I had various issues with the clientclick, when trying to perform a simple confirm check, my code is on_init.  the thing is I don't think you can get rid of the dopostback default generated code, at least I hadn't managed.

The only way i managed to get it to work was to cheat a little and formulated my code in this manner:  

btndelete.ClientClick = "if (confirm(\'Are you sure you want to delete?\')){}else{return false}";


(i had tried "return confirm()" directly - didn't work, tried with a function, in which i would return true or false - didn't work either, and neither in other cleaner ways. )

What this does is writes the "return false" string before the ";_dopostback();" string if you reject. Otherwise it writes nothing and performs the postback.  In your case, If i understood correctly, you want to perform the function and write "return false", just the same to stop the postback.  Don't do the last ; as it is included automatically.

If you manage to find a cleaner way.. do let me know! :) as i am not to fond of this.


Dec 20, 2011 at 1:51 PM


thanks for the tipp! I also found out another way to get it working:


Define a static Method in a .aspx file which returns the jscript function includign the dynamic values as a string:

public static String GetJavaScriptFunctionString()
    return "javascriptFunction(+someDynamicInfo.ToString()+)";

Enable PageMethods in your .Master -> EnablePageMethods="true"

Now you can assign this dynamic methods in the mark up part statically as follows:

ClientClick="PageMethods.GetJAvaScriptFunctionString() return false"