Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

How to disable button after click or during postback and how to change button text after click or during postback in asp.net

Nov 25, 2010
Introduction

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.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

11 comments :

zia said...

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

zia said...

try posting some advanced tuts bro

Suresh Dasari said...

Thanks Zia sure i will learn and post advanced topics

K@rTh!K said...

Microsoft Jscript runtime error:
'sys' undefined

Rathinamoorthy said...

Much useful for the Freshers. Thanks a lot bro :)

Anonymous said...

not working properly. no effect on button..

Unknown said...

only work in Internet Explorer !

vamsi said...
This comment has been removed by the author.
Unknown said...

not working in all browser ...

Anonymous said...

nice

rajesh said...

hi,
this code is not working in chrome.

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.