2

我有一个非常奇怪的单选按钮列表问题,它可以正常工作,但点击几下后它似乎没有触发 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
4

1 回答 1

1

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

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

在 Page_Load

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

现在您的 SelectedIndexChanged 代码将是

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
}
于 2013-03-17T15:55:19.973 回答