繁体   English   中英

使用JavaScript将值设置为下拉列表,然后在C#中获取该值

[英]Set the value to drop down list using Javascript and get that value in C#

    function SetDropDownValue() {
            var opt = document.createElement("option");
            opt.text = "New Value";
            opt.value = "New Value";
            document.getElementById('<%=DropDownList.ClientID%>').options.add(opt);
            document.getElementById('<%=DropDownList.ClientID%>').value = val;
    }

上面的编码对我有用,单击按钮时,新值将附加到下拉列表中。 然后我想在我的代码后面(C#)中获得下拉值。 它在C#中不起作用。

    string res = DropDownList.SelectedValue;

在C#编码中,仅显示空字符串(“”)。

我如何获得下拉菜单选择的值?

您正在更改html但没有更改DropDownListViewState ,这就是为什么您没有获取asp.net服务器端code 您可以在某个隐藏字段中添加该值,并在服务器端使用它。

HTML

<input type="hidden" id="hdnDDL" runat="server" />

使用Javascript

function SetDropDownValue() {
    var opt = document.createElement("option");
    opt.text = "New Value";
    opt.value = "New Value";
    document.getElementById('<%=DropDownList.ClientID%>').options.add(opt);
    document.getElementById('<%=DropDownList.ClientID%>').value = val;
    document.getElementById('<%=hdnDDL.ClientID%>').value = val;
}

后面的代码

string optionValue = hdnDDL.Value;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM