Friday, April 20, 2012

Adding Please Wait message to a Button

In this post am going to explain how to add a please wait button for a asp button . for This we are using two methods called GetPostBackEventReference() and RegisterClientScriptBlock() of client script.
To do this first we will create a new aspx page with the following aspx code

<html xmlns="">
<head id="Head1" runat="server">
   <title>Untitled Page</title>
   <form id="form1" runat="server">
               <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />

After adding the code please add the following name spaces on the top

using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

After adding the Name spaces add the following code in the code behind :

protected void Page_Load(object sender, EventArgs e)
       Button1.Attributes.Add("onclick", ClientScript.GetPostBackEventReference(Button1, "") + ";this.value='Please wait...';this.disabled = true;");

        protected void Button1_Click(object sender, EventArgs e)
       ClientScript.RegisterClientScriptBlock(this.GetType(), "reset","document.getElementById('" + Button1.ClientID + "').disabled=false;", true);

Output :

No comments:

Post a Comment