单选按钮列表事件不会一直触发

时间:2013-03-17 14:45:59

标签: c# asp.net radiobuttonlist

我有一个非常奇怪的问题,一个单选按钮列表,它工作正常,但点击几下它似乎没有触发SelectedIndexChanged事件,并在回发后保持相同的值。

<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>  

以下是事件方法:

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.
    }

我不明白为什么它只能正常工作3次但之后,它从未进入上述方法。

使用下拉列表尝试相同的操作,也可以使用3次,然后出现此错误:

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

1 个答案:

答案 0 :(得分:1)

根据您的上一条评论,删除将SQL查询设置为SelectedItem.Value的代码,并在需要时使用SelectedItem.Text属性获取命令,select查询可能包含>,{{1}等字符}等会导致无效的回发错误,您可以将代码更改为以下内容:

<

在Page_Load

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 "";
    }
}

现在您的SelectedIndexChanged代码将是

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