2012年3月10日土曜日

Run Javascript code before UpdatingPanel

You can attach client side javascript to your button like this:

btnTrigger.Attributes["onclick"] = "javascript:if(confirm('should I postback')){ return true;}else{return false;}";

if the javascript returns true it continues with postback to run the server side code else it does not post back.

In your case you would call whatever javascript functions you need from the .js file then return true

Hope it helps,

Joe

Joe, I think this will work great. My question to you is this:

If my aspx does NOT have a master page, The ID attribute that I assign to my <ASP:Button> goes out to the browser as is. But since my aspx has a master page, when I build an <ASP:Button>, and I assign it an ID (btn1), I notice that the ID is going to out the browswer as something else (ctl00$btn1)

I know this is an asp.net thing to ensure unique id's, but how am I suppose to write JS functions and make DOM references if I don't know what my element ID is? do you have any thoughts on how to get around this issue? can my trigger be an HTML element (without the runat="server" attribute?)

Thanks for your great help.


one solution is to use the ClientID property from the code behind to render a javascript helper method to get your object. - this code is not tested

string script = "function GetButtonID { return " + this.btn1.ClientID + "; }";

Page.ClientScript.RegisterClientScriptBlock(typeof(string), "test", script);

then in javascript use the GetButtonID method as needed to get DOM reference, etc...


Thanks for the help guys.

0 件のコメント:

コメントを投稿