Saturday, May 31, 2014

Radio Button Control

Properties of Radio Button Control
    Checked – This is Boolean property, that is used to check if the button is checked or not.
    Text – This is string property used to get or set the text associated with the radio button control.
    AutoPostBack – Set this property to true, if you want the web form to be posted immediately when the checked status of the radio button changes.
    Group Name – By default the individual radio button selections, are not mutually exclusive. If you have a group of radio buttons, and if you want the selections among the group to be mutually exclusive, then use the same group name for all the radio button controls.

    CheckedChanged– This is fired when the checked status of the radio button control is changed.
Here an example to print text in the textbox
WebForm1.aspx :-
 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="_11_RadioButtonControl.WebForm1" %>  
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">  
 <html xmlns="">  
 <head runat="server">  
   <form id="form1" runat="server">  
     <fieldset style="width:300px">  
       <asp:RadioButton ID="RadioButton1" runat="server" GroupName="Gender"   
         Text="Male" />  
       <asp:RadioButton ID="RadioButton2" runat="server" GroupName="Gender"   
         Text="Female" />  
       <br />  
       <asp:RadioButton ID="RadioButton3" runat="server" GroupName="Gender"   
         oncheckedchanged="RadioButton3_CheckedChanged" Text="Unknown" />  
       <br />  
       <br />  
       <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />  

WebForm1.aspx.cs :-
 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Web;  
 using System.Web.UI;  
 using System.Web.UI.WebControls;  
 namespace _11_RadioButtonControl  
   public partial class WebForm1 : System.Web.UI.Page  
     protected void Button1_Click(object sender, EventArgs e)  
       if (RadioButton1.Checked)  
         Response.Write("Gender is "+RadioButton1.Text);  
       else if(RadioButton2.Checked)  
         Response.Write("Gender is " + RadioButton2.Text);  
         Response.Write("Gender is " + RadioButton3.Text);  

No comments:

Post a Comment