Search Engine

Tuesday, May 18, 2010

ASP.Net RadioButtonList OnSelectedIndexChanged Event

The OnSelectedIndexChanged event handler of ASP.Net RadioButtonList control enables you to handle the click event of radiobuttonlist item at server side that is raised at the time when user clicks the radio button list list item to change its inactive state to selected/checked state. The SelectedIndexChanged event of RadioButtonList control occurs only if the AutoPostBack property of radiobuttonlist is specified with value "true". The SelectedIndexChanged event occurs immediately when the checked state of the list item of radiobuttonlist changes and it submits the web form to the server only if the AutoPostBack property has value "true". You can handle the event at server side using C# code and apply the custom logic there to execute any specific action.

Sample Code for ASP.Net RadioButtonList OnSelectedIndexChanged Event

In the above sample code for RadioButtonList control we have specified the AutoPostBack property value as "true" and also OnSelectedIndexChanged server side event is used to handle the click event of radiobutton list item when user clicks the control to change its checked state.

You can attach the SelectedIndexChanged of RadioButtonList control in two ways as follows:

1. Double clicking the RadioButtonList control for which you want to generate the server side OnSelectedIndexChanged event handler. In this tutorial we have also used the same approach to handle the SelectedIndexChanged event of RadioButtonList control.

2. You can also attach the SelectedIndexChanged event programmatically in the page load method of ASP.Net web page E.g.:

protected void Page_Load(object sender, EventArgs e)
RadioButtonList1.SelectedIndexChanged += new EventHandler(RadioButtonList1_SelectedIndexChanged);

void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
throw new Exception("The method or operation is not implemented.");

When you will write the ID of the radiobuttonlist control inside the Page_Load method for example RadioButtonList1, and will type the "." Dot operator it will display the list of properties and methods of radiobuttonlist control. Select the SelectedIndexChanged event from the intellisense and press "+=" and then tab key from the keyboard. It will generate the event handler automatically.

C# code:

protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
Label1.Text = "Selected Index: " + RadioButtonList1.SelectedIndex.ToString() + "
Label1.Text += "Selected Value: " + RadioButtonList1.SelectedValue + "";

In the above C# sample code for SelectIndexChanged event handler of ASP.Net RadioButtonList control we have retrieved the values of its two properties such as SelectedIndex and SelectedValue. The SelectedIndex property of RadioButtonList control returns the index of the list item clicked by the user where as SelectedValue property returns the value of the selected radio button type list item of the radiobuttonlist control.


Post a Comment