Sunday, May 6, 2012

Disable a submit button during Post Backs

Download Files:
 

This blog is about, how to restrict multiple button submission or clicks?
Most of the developers facing the issue of how to handle multiple button clicks or submission by user at the same time. 
This can be achieved  by using method ClientScript.GetPostBackEventReference (for more information, refer msdn article).
"Returns a string that can be used in a client event to cause post back to the server. The reference string is defined by 
the specified control that handles the post back and a string argument of additional event information."

Lets start with an sample. Drag n drop three Button control to form. One inside UpdatePanel and the other outside of the UpdatePanel.

#.aspx source

111.jpg

Now the code behind part. We will bind this method on Page Load.
Also, am using Thread.Sleep for 5 sec delay.
22.jpg

Note: The second button will do a full post back. The third button, we can even attach JavaScript validation check before calling get post back event handler.

Button1: 

Button1.gif

Button2: above same button effect, but a full post back will happen.

Button3: first it will show the alert followed by above same button effect.

Button3.gif

Hope this blog helped you. Please post your comments. Sample codes attached for download.

ex. 
lbtnPrint.Attributes.Add("onclick", "this.style.backgroundColor='#6299c5'; this.disabled=true;" + ClientScript.GetPostBackEventReference(lbtnPrint, "").ToString());

No comments:

Post a Comment