Search code examples
c#asp.netradiobuttonlist

radio button list event not firing all the time


I have a very strange issue with a radio button list where it works fine but after a few clicks it doesn't seem to fire the SelectedIndexChanged event and just remains on the same value after postback.

<asp:RadioButtonList runat="server" ID="rblShowRecords" AutoPostBack="true" 
OnSelectedIndexChanged="rblShowRecords_SelectedIndexChanged" RepeatDirection="Horizontal">
    <asp:ListItem >Show Active/Completed</asp:ListItem>
    <asp:ListItem >Show Active</asp:ListItem>
    <asp:ListItem >Show Completed</asp:ListItem>
</asp:RadioButtonList>  

Here is the event method:

protected void rblShowRecords_SelectedIndexChanged(object sender, EventArgs e)
    {

        switch (rblShowRecords.SelectedItem.Text)
        {
            case "Show Active/Completed":
                CEDatabaseSource.SelectCommand = ConfigurationManager.AppSettings["SelectAllRecords"].ToString();//"SELECT * FROM [CERecord] ORDER BY [Priority]";
                break;
            case "Show Active":
                CEDatabaseSource.SelectCommand = ConfigurationManager.AppSettings["SelectActiveRecords"].ToString();
                break;
            case "Show Completed":
                CEDatabaseSource.SelectCommand = ConfigurationManager.AppSettings["SelectCompletedRecords"].ToString();
                break;
            default:
                break;
        }
        CEDatabaseSource.DataBind(); //Commit the changes to the data source.
        gvRecordList.DataBind(); //Update the GridView
        rblShowRecords.SelectedItem.Value = CEDatabaseSource.SelectCommand; //Update the value of the selected radio button with the selected SELECT command.
    }

I don't understand why it only works precisely 3 times but after, it never enters the method above.

Trying the same thing but with a dropdownlist, also works 3 times and then this error:

Invalid postback or callback argument.  Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page.  For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them.  If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation

Solution

  • Based on your last comment, remove your code that sets SQL queries to SelectedItem.Value and use SelectedItem.Text property to get command when you need it, select queries may contain characters like > , <, etc that will cause invalid postback error, you can change your code to following:

    string GetCommand()
    {
        switch (rblShowRecords.SelectedItem.Text)
        {
            case "Show Active/Completed":
                return ConfigurationManager.AppSettings["SelectAllRecords"].ToString();
            case "Show Active":
                return  ConfigurationManager.AppSettings["SelectActiveRecords"].ToString();
            case "Show Completed":
                return  ConfigurationManager.AppSettings["SelectCompletedRecords"].ToString();
            default:
                return "";
        }
    }
    

    In Page_Load

    if (IsPostBack) 
    { 
        CEDatabaseSource.SelectCommand = GetCommand();
        CEDatabaseSource.DataBind(); 
    }
    

    Now your SelectedIndexChanged code will be

    protected void rblShowRecords_SelectedIndexChanged(object sender, EventArgs e)
    {
        CEDatabaseSource.SelectCommand = GetCommand();
        CEDatabaseSource.DataBind(); //Commit the changes to the data source.
        gvRecordList.DataBind(); //Update the GridView
    }