RadioButton Control in Asp.net C# Web Form

Introduction to RadioButton Control:

RadioButton Control: RadioButton Control is used when you want the user to select only one option from available choices.

1. Create a new web form.

2. Drag a radio button from toolbox.

Properties:

a. Set its ID.

b. Set its Text that you want to display to the user as an option.

c. Checked is a boolean property you can set it true if you want the radio button to checked by default.

d. You can set TextAlign property left or right based upon on which side of the control you want to display the text.

e. You can set GroupName property to same name for all radio buttons to make them mutually exclusive.

f. Set its AutoPostBack property to true if you want to call a backend function whenever it is clicked or checked.

g. You can its style properties like CssClass, ForeColor, BackColor, BorderStyle, BorderColor, Height, Width, Font-Bold, Font-Size etc.

    <form id="form1" runat="server">
        <div style="align-content: center; text-align: center; font-size: large">
            <br />
            <br />
            <br />
            <br />
            Select Gender:<br />
            <asp:RadioButton ID="rBtnMale" runat="server" Text="Male" GroupName="Gender" ForeColor="Blue" Checked="true" Font-Bold="true"
                Font-Size="Large" Height="100" Width="500" AutoPostBack="true" OnCheckedChanged="rBtnMale_CheckedChanged" /><br />
            <asp:RadioButton ID="rBtnFemale" runat="server" Text="Female" GroupName="Gender" ForeColor="Blue" Font-Bold="true"
                Font-Size="Large" Height="100" Width="500" AutoPostBack="true" OnCheckedChanged="rBtnFemale_CheckedChanged" />
        </div>
    </form>

Functions:

Most common function of RadioButton is OnCheckedChanged, generate an OnCheckedChanged function by double clicking the button in design view.

Now in the function generated in .aspx.cs file you can program your logic.

Response.Write allow us to write something on top of page from backend.
We can also checked that if RadioButton is checked or not and change its properties accordingly, we can also checked that in PageLoad.

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (rBtnMale.Checked == true)
                {
                    rBtnMale.ForeColor = System.Drawing.Color.Green;
                    rBtnFemale.ForeColor = System.Drawing.Color.Blue;
                }
                else
                {

                }
            }
        }

        protected void rBtnMale_CheckedChanged(object sender, EventArgs e)
        {
            string text = rBtnMale.Text;
            Response.Write("<div style='align-content:center; text-align:center'><br /><br /><br />");
            Response.Write("You checked " + text + "<br/><br/>");
            Response.Write("</div>");
            if (rBtnMale.Checked == true)
            {
                rBtnMale.ForeColor = System.Drawing.Color.Green;
                rBtnFemale.ForeColor = System.Drawing.Color.Blue;
            }
            else
            {

            }
        }

        protected void rBtnFemale_CheckedChanged(object sender, EventArgs e)
        {
            string text = rBtnFemale.Text;
            Response.Write("<div style='align-content:center; text-align:center'><br /><br /><br />");
            Response.Write("You checked " + text + "<br/><br/>");
            Response.Write("</div>");
            if (rBtnFemale.Checked == true)
            {
                rBtnFemale.ForeColor = System.Drawing.Color.Green;
                rBtnMale.ForeColor = System.Drawing.Color.Blue;
            }
            else
            {

            }
        }

Done!
Run the program.
Click the radio buttons and see the call of backend functions.



Let me know in the comment section if you have any question.

Previous Post:
LinkButton Control in Asp.net C# Web Form