"回发或回调参数无效"解决方法

<asp:DropDownList ID="DropDownList1" runat="server">
    <asp:ListItem Value="20">张三</asp:ListItem>
    <asp:ListItem Value="21">李四</asp:ListItem>
</asp:DropDownList>
<input id="btn" type="button" value="客户端添加项" onclick="document.getElementById('DropDownList1').add(new Option('王五','22'));" /><br />
<asp:Button ID="Button1" runat="server" Text="提交" OnClick="Button1_Click"></asp:Button>

服务器端使用 DropDownList1.SelectedValue 获取选择项的值:

protected void Button1_Click(object sender, EventArgs e)
{
    Response.Write(DropDownList1.SelectedValue);
}

当选择项是"张三"或"李四"时程序运行正常,当选择项是使用客户端js增加的"王五"时,程序运行出错:

回发或回调参数无效。在配置中使用 <pages enableEventValidation="true"/> 或在页面中使用 <%@ Page EnableEventValidation="true" %> 启用了事件验证。出于安全目的,此功能验证回发或回调事件的参数是否来源于最初呈现这些事件的服务器控件。如果数据有效并且是预期的,则使用 ClientScriptManager.RegisterForEventValidation 方法来注册回发或回调数据以进行验证。

将EnableEventValidation的值设置为false后,程序不再出错,但在服务器端也无法获取"王五"这一项的值!改用 Request["DropDownList1"] 才能正常获取值:

protected void Button1_Click(object sender, EventArgs e)
{
    Response.Write(Request["DropDownList1"].ToString());
}


评论: 0 | 引用: 0 | 查看次数: 3880
发表评论
登录后再发表评论!