Tuesday, April 26, 2011

Stop subsequent submit button clicks when a earlier request is being processed in ASP.NET

How we can stop the subsequent click when a earlier request is still been processed.

here is the javascript code you need to place above your update panel.
< script type="text/javascript" language="javascript" >
var requestManager = Sys.WebForms.PageRequestManager.getInstance();
requestManager.add_initializeRequest(StopPostbackOnSubsequentSubmitClicks);

function StopPostbackOnSubsequentSubmitClicks(sender, args) {
if (requestManager.get_isInAsyncPostBack() &
args.get_postBackElement().id == '<%=btnSubmit.ClientID %>')
{
args.set_cancel(true);
alert('Please wait, earlier request is still being processed');
}
}
< /script >

Now write your update panel code
< asp:UpdatePanel runat="server" ID="upnlLogin" UpdateMode="Conditional" >
< ContentTemplate >

// do whatever you need

< asp:Button ID="btnSubmit" runat="server" Text="Sign in" CssClass="button" OnClick="btnSubmit_Click" />

< /ContentTemplate>

< /asp:UpdatePanel>
< asp:UpdateProgress AssociatedUpdatePanelID="upnlLogin" runat="server" >
< ProgressTemplate>
< img src="../Images/spinner.gif" / >
< /ProgressTemplate >
< /asp:UpdateProgress >

// Now see the code behind implementation

protected void btnSubmit_Click(object sender, EventArgs e)
{

Thread.Sleep(1000);
}

No comments: