The OnCheckedChanged event handler of ASP.Net RadioButton control enables you to handle the click event of radio button at server side that is raised at the time when user clicks the control to change its in-active state. The CheckedChanged event of radio button control occurs only if its AutoPostBack property is specified with value "true". You can handle the click events of multiple radio button controls separately and perform the server side action for each control using their dedicate OnCheckedChaneged event handlers. The CheckedChanged event occurs immediately when user clicks the radio button control for selecting the item and submits the web form to the server only if the AutoPostBack property has value "true".
Sample Code for ASP.Net RadioButton OnCheckedChanged Event
we have used two radio buttons specified with their AutoPostBack property value as "true" and also OnCheckedChanged server side event is used to handle the click event of each radio button separately when user clicks the control to change its checked state.
You can attach the CheckedEvent of radio button control in two ways as follows:
1. Double clicking the radio button control for which you want to generate the server side OnCheckedChanged event handler. In this tutorial we have also used the same approach to handle the CheckedChanged event of radio buttons.
2. You can also attach the CheckedChanged event programmatically in the page load method of ASP.Net web page E.g.:
{
RadioButton1.CheckedChanged += new EventHandler(RadioButton1_CheckedChanged);
}
void RadioButton1_CheckedChanged (object sender, EventArgs e)
{
throw new Exception("The method or operation is not implemented.");
}
When you will write the ID of the radio button control inside the Page_Load method for example RadioButton1, and will type the "." Dot operator it will display the list of properties and methods of radiobutton control. Select the CheckedChanged event from the intellisense and press "+=" and then tab key from the keyboard. It will generate the event handler automatically.
C# code:
{
Panel1.Visible = RadioButton1.Checked;
Panel2.Visible = !RadioButton1.Checked;
}
protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
{
Panel1.Visible = !RadioButton2.Checked;
Panel2.Visible = RadioButton2.Checked;
}
The above server side event handlers will handle the checked changed event of both radio button controls individually that we placed on the web page as discussed above. Other than radio button controls we have used two ASP.Net Panel controls also to illustrate the additional functionality that you can combine with the checked changed event of radio button controls. When you will click the first radio button then it will hide the second panel control and will display only first Panel control as an associated control to it. Whereas when you will click the second radio button control then its CheckedChanged handler code will execute and will set the visible state of the second panel control to true and first panel control to false.
0 comments:
Post a Comment