Menu ▼



Posted By :
avatarmrkraju
Total Views : 3855

Asp.net radio button example

Asp.net radio button is used to allow user to select any one of option among multiple. For example allowing user to select gender either male or female. And you can call server side c# code (postback) when the user select the radio button. To do that you have to specify the AutoPostBack="true" and implement the OnCheckedChanged="rbtn_CheckedChanged". Below example will let you know how to implement it.

Allowing user to select single option
To allow user to select single option from multiple you need to set the GroupName. For example if you want to allow user to select gender either male or female then you have to set GroupName="gender" for both the radio buttons (Radio buttons which are not set the GroupName property can be selected multiple).

<asp:RadioButton ID="rbtnMale" runat="server" GroupName="gender" Text="Male" />

<asp:RadioButton ID="rtbnFemale" runat="server" GroupName="gender" Text="Female" />

Reading asp.net radio button value (selected or not) in c#
RadioButton.Checked property will return true value when the radio button is selected, other wise it will return false.

if(rbtnIndia.Checked)
{

}

Calling server side code (radio button postback event) in asp.net

protected void rbtn_CheckedChanged(object sender, EventArgs e)
{
    DataTable dt = GetData();
    if (rbtnIndia.Checked)
        dt.DefaultView.RowFilter="CountryID=1";            
    else            
        dt.DefaultView.RowFilter = "CountryID=2";            

    ddlStates.DataSource = dt.DefaultView.ToTable();
    ddlStates.DataTextField = "StateName";
    ddlStates.DataValueField = "StateID";
    ddlStates.DataBind();
}

Example
asp.net radio button example.

ASPX Page

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:RadioButton ID="rbtnMale" runat="server" GroupName="gender" Text="Male" />
        <br />
        <asp:RadioButton ID="rtbnFemale" runat="server" GroupName="gender" Text="Female" />
        <br />
        <br />
        <br />
        <asp:RadioButton ID="rbtnIndia" runat="server" GroupName="country" Text="India" AutoPostBack="true"
            OnCheckedChanged="rbtn_CheckedChanged" />
        <br />
        <asp:RadioButton ID="rbtnUSA" runat="server" GroupName="country" Text="USA" AutoPostBack="true"
            OnCheckedChanged="rbtn_CheckedChanged" />
        <br />
        <br />
        States:<br />
        <asp:DropDownList ID="ddlStates" runat="server">
        </asp:DropDownList>
    </div>
    </form>
</body>
</html>

C# Page

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.Text;
using System.Globalization;

namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {


        }

        DataTable GetData()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("CountryID", typeof(int));
            dt.Columns.Add("CountryName", typeof(string));
            dt.Columns.Add("StateID", typeof(int));
            dt.Columns.Add("StateName", typeof(string));
            dt.Rows.Add(1, "India", 1, "Andhra Pradesh");
            dt.Rows.Add(1, "India", 2, "Karnataka");
            dt.Rows.Add(1, "India", 3, "Maharashtra");
            dt.Rows.Add(2, "USA", 4, "California");
            dt.Rows.Add(2, "USA", 5, "New Jersey");
            dt.Rows.Add(2, "USA", 6, "Texas");
            return dt;
        }

        protected void rbtn_CheckedChanged(object sender, EventArgs e)
        {
            DataTable dt = GetData();
            if (rbtnIndia.Checked)
                dt.DefaultView.RowFilter = "CountryID=1";
            else
                dt.DefaultView.RowFilter = "CountryID=2";

            ddlStates.DataSource = dt.DefaultView.ToTable();
            ddlStates.DataTextField = "StateName";
            ddlStates.DataValueField = "StateID";
            ddlStates.DataBind();
        }

    }

}






comments powered by Disqus


Search dotnetlearners.com