javascript - Page.EnableEventValidation = false 无法识别新的下拉列表值

标签 javascript c# asp.net visual-studio-2010 asp.net-4.0

我的测试网络表单有 1 个下拉列表和 2 个按钮。就是这样:

<form id="form1" runat="server">
    <asp:DropDownList ID="DropDownListTest" runat="server" >
        <asp:ListItem>Value 0</asp:ListItem>
        <asp:ListItem>Value 1</asp:ListItem>
        <asp:ListItem>Value 2</asp:ListItem>
    </asp:DropDownList>

<asp:Button ID="ButtonClient" runat="server" Text="Button" OnClientClick="ChangeDropDown(); return false;" />

<asp:Button ID="ButtonServer" runat="server" Text="Server" onclick="ButtonServer_Click" />
</form>

这是javascript函数ChangeDropDown():

function ChangeDropDown() {
        for (i = document.getElementById("<%=DropDownListTest.ClientID%>").options.length - 1; i >= 0; i--) {
            document.getElementById("<%=DropDownListTest.ClientID%>").remove(i);
        }
        var opt = document.createElement("option");

        opt.text = "Complete";
        opt.value = "Complete";
        document.getElementById("<%=DropDownListTest.ClientID%>").options.add(opt);


        var opt2 = document.createElement("option");
        opt2.text = "Not Complete";
        opt2.value = "Not Complete";
        document.getElementById("<%=DropDownListTest.ClientID%>").options.add(opt2);
} 

这是隐藏代码:

    protected void ButtonServer_Click(object sender, EventArgs e)
    {
        string val = this.DropDownListTest.SelectedValue;
    }
    protected void Page_PreInit(object sender, EventArgs e)
    {
        Page.EnableEventValidation = false;
        //"Invalid postback or callback argument" if I remove this.
    }

假设我单击按钮 ButtonClient(这会更改下拉列表的值),选择“完成”,然后单击 ButtonServer。在代码隐藏中,this.DropDownListTest.SelectedValue 的值等于“Value 0”而不是“Complete”。

我明白为什么会遇到这个问题,但是除了添加项目代码隐藏之外还有其他解决方案吗?

最佳答案

您无法以标准方式获取该值,因为服务器不知道下拉列表中的项目已更改。

但是,您可以直接从请求中获取值:

protected void ButtonServer_Click(object sender, EventArgs e)
{
    string val = Request[this.DropDownListTest.UniqueID];
}

关于javascript - Page.EnableEventValidation = false 无法识别新的下拉列表值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33001506/

相关文章:

javascript - 使用 GitHub API 编辑提交消息并将其添加到文件

javascript - NodeJS child_process stdout,如果进程正在等待 stdin

c# - 如何更改剑道错误信息?

asp.net - 如何确保上传的文件内容使用后无法恢复?

c# - ASP.Net/C# : Replacing characters in a databound field

javascript - JQuery 隐藏除我搜索的 div 之外的所有 div

javascript - 创建时访问对象内容

c# - 我可以使用 C# 集合来保存具有自引用关系的类实例吗?

c# - 删除 EWS 中的 Exchange 事件修改/删除(即恢复事件)

c# - .net 结束打印事件