【发布时间】:2013-03-17 23:59:36
【问题描述】:
我有一个非常奇怪的单选按钮列表问题,它可以正常工作,但单击几下后似乎不会触发 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
【问题讨论】:
-
当您在最后一行代码中为项目设置值时,您试图将 SQL 查询设置为项目值,并且 SQL 查询可能包含会导致无效回发的字符。您已经知道要根据文本检索哪个选择命令,那么为什么要尝试将选择查询设置为下拉列表?
-
那么 selected.item.text 就是您在 DDL 中看到的内容,而 selected.item.value 是保存在配置文件中的字符串 sql 查询。但是为什么它工作了 3 次然后就失败了?
-
是的,但为什么要将其设置为 Selected.item.Value?并且您可能有一个查询包含像
>或<这样的字符,这些字符是无效的控制值,会导致无效的回发 -
基本上我需要在 page_load
if (IsPostBack) { CEDatabaseSource.SelectCommand = ddlShowRecords.SelectedValue; CEDatabaseSource.DataBind(); }中执行此操作,否则当我有某个视图并单击一行进行编辑时,gridview 会调用默认选择命令并将所有内容搞砸
标签: c# asp.net radiobuttonlist