Tuesday, September 14, 2010

Disable a submit button during Post Back In asp.net

--- Add these line on aspx page ---------------

<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
  <ContentTemplate>
     <asp:Button ID="Button1" Text="Submit" runat="server" onclick="Button1_Click" />
  </ContentTemplate>
</asp:UpdatePanel><br />
<asp:Button ID="Button2" Text="Save (without UpdatePanel)" runat="server" onclick="Button1_Click" />
<br /><br />
<asp:Button ID="Button3" Text="Save (with showing alert)" runat="server" onclick="Button1_Click" />

----------------------------------------------
---- Add thses live cs page


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    String sString = "this.style.backgroundColor='skyblue'; this.style.color='Red'; this.value='Saving Records... Please Wait...'; this.disabled = true; {0};";
    String sHandler = String.Format(sString, this.ClientScript.GetPostBackEventReference(Button1,
    String.Empty));
    Button1.Attributes.Add("onclick", sHandler);

    sHandler = "";
    sHandler = String.Format(sString, this.ClientScript.GetPostBackEventReference(Button2,
    String.Empty));
    Button2.Attributes.Add("onclick", sHandler);

    sHandler = "";
    sHandler = String.Format(sString, this.ClientScript.GetPostBackEventReference(Button3,
    String.Empty));
    Button3.Attributes.Add("onclick", "javascript:alert('hello');" + sHandler);
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        System.Threading.Thread.Sleep(5000);
    }
}

No comments:

Post a Comment