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

Enable and disable RadioButton programmatically
RadioButton is an asp.net web server control. radiobutton has a built in property to enable or disable itself programmatically at run time.

radiobutton Enabled property get or set a value indicating whether the radiobutton control is enabled. the Enabled property value type is System.Boolean. we can only set one of two possible values true or false for this property. if we set the Enabled property value to 'true' then it will enabled the radionbutton control on web page. and if we set the radiobutton Enabled property value to false then it will render the radiobutton as a disabled control. generally a disabled control appear as dimmed in web browser.

the following asp.net c# example code demonstrate us how can we enable or disable radiobutton control dynamically at run time in an asp.net application.
RadioButtonDisable.aspx
<%@ Page Language="C#" %>

<!DOCTYPE html>

<script runat="server">
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        RadioButton1.Enabled = false;
        RadioButton2.Enabled = false;
    }
    protected void Button2_Click(object sender, System.EventArgs e)
    {
        RadioButton1.Enabled = true;
        RadioButton2.Enabled = true;
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to enable, disable RadioButton programmatically</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Navy">RadioButton Example: Enable, Disable</h2>
        <asp:Label 
             ID="Label1"
             runat="server"
             Font-Bold="true"
             ForeColor="OliveDrab"
             Text="Favorite Color?"
             >
        </asp:Label>
        <br />
        <asp:RadioButton 
             ID="RadioButton1" 
             runat="server"
             Text="LightBlue"
             GroupName="Colors"
             />
        <asp:RadioButton 
             ID="RadioButton2" 
             runat="server"
             Text="IndianRed"
             GroupName="Colors"
             />
        <br /><br />           
        <asp:Button 
             ID="Button1" 
             runat="server" 
             ForeColor="OliveDrab"
             Text="Disable RadioButton"
             OnClick="Button1_Click"
             Font-Bold="true"
             />
        <asp:Button 
             ID="Button2" 
             runat="server" 
             Font-Bold="true"
             ForeColor="OliveDrab"
             Text="Enable RadioButton"
             OnClick="Button2_Click"
             />
    </div>
    </form>
</body>
</html>








Related asp.net examples