Here I will explain how to disable button after click or during postback and I will explain how to change the button text after click on button or during postback in asp.net
Description
In many websites we will see disable of button after click on that for getting some content how we can implement that in our application.
Here we need to observe one point that is how we can findout button click and how we can disable button and how we can change text of button during postbacks in asp.net. Here we have chance to get postsbacks by using these statements
Sys.WebForms.PageRequestManager.getInstance(); Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequestHandler); Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler); |
Here I will explain what the each statement is
Sys.WebForms.PageRequestManager.getInstance();
This Statement Manages partial-page updates of server updatepanel controls in the browser, and defines properties, events, and methods that can be used to customize a Web page by using client script.
Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequestHandler);
This statement Raised before processing of an asynchronous postback starts and the postback request is sent to the server.
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
This Raised after an asynchronous postback is finished and control has been returned to the browser.
After that design your aspx page like this
<html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server"> <title>Disable Button and Change Text of button</title> </head> <body> <form id="form1" runat="server"> <asp:ScriptManager ID="sc" runat="server" EnablePartialRendering="true"> </asp:ScriptManager> <script type="text/javascript"> Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequestHandler); function BeginRequestHandler(sender, args) { document.getElementById('<%= lblMessage.ClientID %>').innerText = "Processing..."; document.getElementById('<%= btnSubmit.ClientID %>').innerText = "Processing"; args.get_postBackElement().disabled = true; } </script> <asp:UpdatePanel ID="updpnlSubmit" runat="server"> <ContentTemplate> <asp:Button ID="btnSubmit" Text="Submit" runat="server" OnClick="btnSubmit_Click" /> <asp:Label ID="lblMessage" runat="server"></asp:Label> </ContentTemplate> </asp:UpdatePanel> </form> </body> </html> |
In code behind button click write like this
protected void btnSubmit_Click(object sender, EventArgs e) { System.Threading.Thread.Sleep(2000); lblMessage.Text = "PostBack completed!!"; } |
Demo
Other related post
how to show progressbar during postback in asp.net
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
|
|||
11 comments :
great tutorials for beginners. althought i know most of the techniques . i have still bookmarked ur site and added your rss feed on google reader for quick reference
try posting some advanced tuts bro
Thanks Zia sure i will learn and post advanced topics
Microsoft Jscript runtime error:
'sys' undefined
Much useful for the Freshers. Thanks a lot bro :)
not working properly. no effect on button..
only work in Internet Explorer !
not working in all browser ...
nice
hi,
this code is not working in chrome.
Note: Only a member of this blog may post a comment.