-->

22 December 2019

Asp.Net RadioButton Control

  Asp.Net CS By Example       22 December 2019

  The RadioButton Control :
     The RadioButton control use in a group. Means Only one radio button in a group of RadioButton controls can be checked at a time.
Syntax:
<asp:RadioButton ID="rdlMagazine" Text="Magazine Article" runat="server"  />

The RadioButton control supports the following properties (this is not a complete list):
S.N. Property Description
1) ID Set the uniqe value to control to access in code-behind code.
2) Text This use for set or get value from or to RadioButton control.
3) AccessKey This property used to specify a key that navigates to the RadioButton control.
4) TextAlign Align the label for the RadioButton. Possible values are Left and Right.
5) AutoPostBack Enables we to post the form containing the RadioButton back to the server automatically
when the RadioButton is checked or unchecked.
6) Enabled This used to enable or disable the RadioButton control.
7) TabIndex Used to set the tab order of the check box.

The CheckBox control also supports the following method:
S.N. Method Description
1) Focus Used to set the initial form focus on it.

The CheckBox control supports the following event:
S.N. Event Description
1) CheckedChanged Raised on the server when the check box is checked or unchecked.
 When the AutoPostBack property has the value True, the form containing the check box is
automatically posted back to the server when the check box is checked or unchecked.

Design Code: RadioButtonDemo01.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="RadioButtonDemo01.aspx.cs" 
                Inherits="LearnAsp.Net.Control.RadioButton.RadioButtonDemo01" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            How did you hear about our Website?
            <ul>
                <li>
                    <asp:RadioButton ID="rdlSocialMedia" Text="Socail Media Sites"
                 GroupName="Source" runat="server" />
                </li>
                <li>
                    <asp:RadioButton ID="rdlMagazine" Text="Magazine Article" 
                GroupName="Source" runat="server" />
                </li>
                <li>
                    <asp:RadioButton ID="rdlTelevision" Text="Television Program" 
                GroupName="Source" runat="server" />
                </li>
                <li>
                    <asp:RadioButton ID="rdlInternetAdd" Text="Internet Advertise" 
                GroupName="Source" runat="server" />
                </li>
                <li>
                    <asp:RadioButton ID="rblNewPaper" Text="News Paper"
                 GroupName="Source" runat="server" />
                </li>
                <li>
                    <asp:RadioButton ID="rdlOther" Text="Other Source"
                 GroupName="Source" runat="server" />
                </li>
            </ul>
            <asp:Button ID="btnSubmit" Text="Submit" runat="server" OnClick="btnSubmit_Click" />
            <hr />
            <asp:Label ID="lblResult" runat="server" />
        </div>
    </form>
</body>
</html>

Code-Behind Code: RadioButtonDemo01.aspx.cs

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

namespace LearnAsp.Net.Control.RadioButton
{
    public partial class RadioButtonDemo01 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (rdlSocialMedia.Checked)
                lblResult.Text = rdlSocialMedia.Text;
            if (rblNewPaper.Checked)
                lblResult.Text = rblNewPaper.Text;
            if (rdlMagazine.Checked)
                lblResult.Text = rdlMagazine.Text;
            if (rdlInternetAdd.Checked)
                lblResult.Text = rdlInternetAdd.Text;
            if (rdlTelevision.Checked)
                lblResult.Text = rdlTelevision.Text;
            if (rdlOther.Checked)
                lblResult.Text = rdlOther.Text;
        }
    }
}

Output:

logoblog

Thanks for reading Asp.Net RadioButton Control

Previous
« Prev Post

No comments:

Post a Comment

Please do not enter any spam link in the comment box.