What to do when RadioButtonList selectedindexchanged event does not fire in asp.net?

TVicky picture TVicky · Jan 8, 2015 · Viewed 10.2k times · Source

I have a radio button list that contains 2 items right now.Here is the aspx code:

<asp:RadioButtonList ID="RadioButtonList1" runat="server" Font-Bold="True" 
                     Height="52px" Width="181px" AutoPostBack="True" EnableTheming="True"
                     EnableViewState="true" onselectedindexchanged="RadioButtonList1_SelectedIndexChanged">
    <asp:ListItem Value="Head of family "></asp:ListItem>
    <asp:ListItem Value="Show all">All Family Members</asp:ListItem>
</asp:RadioButtonList>

On page load when the page is loaded for the first time,I have set the first radio button item as the selected item through the following code :

protected void Page_Load(object sender, EventArgs e)
{
    try
    {
        if (!IsPostBack)
        {                
            RadioButtonList1.Items[0].Selected = true;              
        }
    }
    catch (Exception ce)
    {
        ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + ce.Message + "');", true);
    }         
}

But after all of this, the selectedindexchanged event does not fire.I tried setting the EnableViewState property to true for the radiobuttonlist as well as for the page.I also tried setting the first item as selected through the aspx code rather than doing it at page load, but nothing worked.What should be done?This is the selectedindexchanged event:

protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{

}

The first item gets selected, there are no issues, but when I try to select the second element, nothing happens apart from a postback. There seems to be an issue with the radio buttons.I tried adding two radio buttons to the page to see if they work or not.Even simple radio buttons are not responding to selections.However when I add checkboxes, they work alright.

Answer

dario picture dario · Jan 8, 2015

If you select the item in the code behind, the event doesn't fire.

From MSDN:

The SelectedIndexChanged event is raised when the selection from the list control changes between posts to the server.

More informations here:

ListControl.OnSelectedIndexChanged Method