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="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
   <title>Untitled Page</title>
</head>
<body>
   <form id="form1" runat="server">
       <div>
           <div>
               <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
           </div>
       </div>
   </form>
</body>
</html>


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)
        {
           System.Threading.Thread.Sleep(5000);
       Button1.Attributes.Add("onclick", ClientScript.GetPostBackEventReference(Button1, "") + ";this.value='Please wait...';this.disabled = true;");
        }

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

Output :


No comments:

Post a Comment