asp.net - How to enable and disable a Button programmatically

How to enable, disable Button programmatically
asp.net button web server control create a push button on web page. by default developer use button as a submit button. button click event submit the web page to the server. so that web developer can process the form in server and return a page to user.

for various criteria, .net developers some time need to disable a button control programmatically. and they need to enable a disabled button programmatically. this example demonstrate how can we enable or disable button web server control programmatically. button Enabled property have two possible values true or false. if you set the Enabled property value false, it will disable the button control and if you set it value true, it will enable the button again. by default asp.net button control is enabled. a disabled button control cannot response any event such as button click event.
ButtonDisable.aspx

<%@ Page Language="C#" %>

<!DOCTYPE html>

<script runat="server">
    protected void Button2_Click(object sender, System.EventArgs e)
    {
         Button1.Enabled = false;
         Label1.Text = "Button Disable";
    }
    protected void Button3_Click(object sender, System.EventArgs e)
    {
        Button1.Enabled = true;
        Label1.Text = "Button enable";
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to enable, disable Button programmatically</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Teal">Button Example: Enable Disable</h2>
        <asp:Label 
             ID="Label1"
             runat="server"
             Font-Bold="true"
             Font-Size="Large"
             ForeColor="OliveDrab"
             >
        </asp:Label>
        <br /><br />
        <asp:Button 
             ID="Button1" 
             runat="server" 
             Text="Test Button Enabled"
             ForeColor="Crimson"
             Font-Bold="true"
             Height="40"
             />
        <br /><br />
        <asp:Button 
             ID="Button2" 
             runat="server" 
             Text="Disable Button"
             OnClick="Button2_Click"
             />
        <asp:Button 
             ID="Button3" 
             runat="server" 
             Text="Enable Button"
             OnClick="Button3_Click"
             />
    </div>
    </form>
</body>
</html>
Related asp.net examples