RadioButton Example in ASP.NET using C#


Posted in : ASP.Net Posted on : November 26, 2010 at 6:31 PM Comments : [ 0 ]
Advertisements
In this article we will introduce with RadioButton control in ASP.NET using C#.

RadioButton Example in ASP.NET using C#

RadioButton web server control is used for select or deselect an item when all these RadioButtons will be in a same group. It is more useful component in ASP.NET. It is similar to CheckBox control but the difference between CheckBox and RadioButton is that you can select or deselect the CheckBox by clicking on it but you will select RadioButton by clicking on it and deselect by clicking on other RadioButton if both RadioButton are in a same group. If you want perform operation on selection of RadioButton then you will true the AutoPostBack. In this example when you select the RadioButton then the result will be shown in Label.

RadioButton.aspx (Design Page):

RadioButton.aspx (source code):

<%@ Page Title="" Language="C#" MasterPageFile="~/RoseindiaMaster.master" 
AutoEventWireup="true" CodeFile="RadioButton.aspx.cs" Inherits="RadioButton" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<div>
<h2 style="color:Green">RadioButton in ASP.NET 4 , C#</h2>
<strong>My age between:
</strong>
<br />
<br />
<asp:RadioButton ID="RadioButton1" Text="10-15 years" GroupName="Age" 
runat="server" OnCheckedChanged="RadioButtonAge_CheckedChanged" AutoPostBack="true"/>
<br />
<asp:RadioButton ID="RadioButton2" Text="15-20 years" GroupName="Age" 
runat="server" OnCheckedChanged="RadioButtonAge_CheckedChanged" AutoPostBack="true"/>
<br />
<asp:RadioButton ID="RadioButton3" Text="20-25 years" GroupName="Age" 
runat="server" OnCheckedChanged="RadioButtonAge_CheckedChanged" AutoPostBack="true"/>
<br />
<asp:RadioButton ID="RadioButton4" Text="25-30 years" GroupName="Age" 
runat="server" OnCheckedChanged="RadioButtonAge_CheckedChanged" AutoPostBack="true"/>
<br />
<br />
<asp:Label ID="Label1" runat="server" BackColor="#FFFF99" Font-Bold="True" 
ForeColor="#000099" />
</div>
</asp:Content>

RadioButton.aspx.cs (C# code file):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class RadioButton : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void RadioButtonAge_CheckedChanged(object sender, System.EventArgs e)
{
if (RadioButton1.Checked == true)
{
Label1.Text = "My age between 10-15 years";
}
else if (RadioButton2.Checked == true)
{
Label1.Text = "My age between 15-20 years";
}
else if (RadioButton3.Checked == true)
{
Label1.Text = "My age between 20-25 years";
}
else if (RadioButton4.Checked == true)
{
Label1.Text = "My age between 25-30 years";
}
}
}

Output:

Download source code
Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Tutorial Topics