ASP.Net RadioButtonList OnSelectedIndexChanged Event

Updated on 24 Jul 2012,
Published on 22 Mar 2010

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

HTML Code
<asp:RadioButtonList ID="RadioButtonList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged">
    <asp:ListItem Value="item 1"></asp:ListItem>
    <asp:ListItem Value="item 2"></asp:ListItem>
    <asp:ListItem Value="item 3"></asp:ListItem>
    <asp:ListItem Value="item 4"></asp:ListItem>
    <asp:ListItem Value="item 5"></asp:ListItem>
</asp:RadioButtonList>
<br />
<asp:Label ID="Label1" runat="server"></asp:Label>

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: <b>" + RadioButtonList1.SelectedIndex.ToString() + "</b><br />";
    Label1.Text += "Selected Value: <b>" + RadioButtonList1.SelectedValue + "</b>";
}

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.

Output:

Get the output for above discussed code from the following link:

RadioButtonList Control OnSelectedIndexChanged Event

Continue to next tutorial: ASP.Net RadioButtonList Control with Sql DataBinding to learn how to display the data retrieved from the SQL database as list items of radiobuttonlist control.

2 Responses to "ASP.Net RadioButtonList OnSelectedIndexChanged Event"
code in radio button in click event send the fast
shilpa
good explination!!!
Leave a Comment
* required
* required
* will not be published
* optional
* hint: http://www.example.com
  • Subscribe via Email