Saturday 12 May 2012

Radio Button

Radio buttons are used when there is a list of two or more item that are mutually exclusive and the user need to select exactly one item. By using radio buttons, clicking a non-selected radio button will deselect whatever other radio button was previously selected in the list of items.To add a radio button into your project goto toolbox and double click on RadioButtons.
For example:

<asp:RadioButton ID="RadioButton1" runat="server" Text= "Radiobutton" Checked="true"/>




Some Useful Properties of Radio Buttons

ID Property:
Radiobutton.ID property id used to set the ID of  Radio button control.


protected void Page_Load(object sender, System.EventArgs e)
{
   // Set a ID to a Radiobutton
    Radiobutton.ID = "chkUserName";
}

Text Property:
We can set a value to  Radiobutton by using   Radiobutton.Text  property.For example


protected void Page_Load(object sender, System.EventArgs e)
{
   // set a text to Radiobutton
     Radiobutton1.Text = "Male";
    Radiobutton2.Text = "Female";
}

Enabled Property:
Radiobutton.Enabled property enable the state of   Radiobutton control..Initially its value is  true.

protected void Button1_Click(object sender, System.EventArgs e)
{
     Radiobutton.Enabled = false;
}

Visible Property:


Radiobutton.Visible property is used to set the visiblity of a  Radiobutton.If its value is true then  Radiobutton will be visible otherwise it will be hide for user.


protected void Button1_Click(object sender, System.EventArgs e)
{
   Radiobutton1.Visible = false;
}

Height & Width Property:
You can use the Height property along with the Width property  to size an object to specific dimensions.


protected void Button1_Click(object sender, System.EventArgs e)
{
     Radiobutton.Height = 100px;
     Radiobutton.Width = 100px;
}


CssClass Property:
You can use the CssClass property to specify the CSS(Cascadding Style Sheet) class to render on the client for the Web Server control. This property will render on browsers for all controls. In simple words you can add CssClass  by using CssClass property by just giving a cssclass name.

<asp:RadioButton ID="checkbox1" runat="server" Text="Subjects"  CssClass="HomePage"></asp:RadioButton>


Some Useful Events of RadioButton


Init Event:
This event fires when the page has been initialized.During page initialization, controls on the page are accessible and each control's UniqueID property is set. A master page and themes are also applied to the page if required. If the current request is a postback, the postback data has not yet been loaded and control property values have not been restored to the values from view state.


protected void  RadioButton_Init(object sender, EventArgs e)
{
   Here you can write your logic...
}

Load Event:

This event fires when the web page has been loaded.During load, the opposite happens to the Init Event, all the postback data has been set and all the controls are ready and can fire their different events. The top container, the content page loads first as it can change the master page and user controls, then the master page and in the end the leaf controls.

protected void  RadioButton _Load(object sender, System.EventArgs e)
{
   Here You can write your code.
}


CheckedChanged Event:
This event occurs when the user changed the checked property of the control.

Some Useful Methods of RadioButton

Focus Method:

How to Set Focus on Asp.Net  ?

Radiobutton.Focus() method is use to set the control on a  Radiobutton.Focus method is helpful to reduce the use of mouse and user can easily get a control by using keyboard.


AT Server Side : 

protected void Page_Load(object sender, EventArgs e)
{
   Radiobutton.Focus();
}


At Client Side:

<%@ Page Language="C#" %>
<html>
<head runat="server">
  <title>Test Page</title>
</head>
<body>
  <form id="form1" runat="server" defaultfocus="TextBox1" >
    <div>
      <asp: Radiobutton  ID="Checkbox1" runat="server"></asp:Radiobutton>
      <br />
      <asp:Button ID="Button1" runat="server" Text="Button" />
      <br />
    </div>
  </form>
</body>
</html>


Databind() Method:
Radiobutton.Databind() binds the datasource to the invoked server control and all its child       controls.


Dispose() Method:
Checkbox.Dispose()  enables a server control to perform a final cleanup before it is released from memory.


To see more properties of Radiobutton click the link given below.
http://msdn.microsoft.com/en-us/library/system.windows.forms.radiobutton_properties.aspx

2 comments: