Friday, February 29, 2008

How to avoid multiple button clicks while post back in ASP.NET

Many time we face the issue how to prevent user from clicking the same button before completing the post back.

A simple java script can be added for this. this will change the text of button and verify. A Dot Net 2.0 example given below


< asp:Button ID="btnAction" runat="server"
OnCommand="ActionEvent"
OnClientClick="
{
var res=this.value=='Processing...'?false:true;
this.value='Processing...';
return res;
}"
Text="Do Action" Width="125px" />

The text of button is changed to 'Processing...' so that the user even knoWS something is going on and even if he clicks it will not post back as our client script returns false if the caption is 'Processing...'.

No comments: