使用Javascript将值设置为下拉列表,然后在C#中获取该值 - 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而不是ViewStateDropDownList,这就是为什么您没有进入asp.net服务器端code的原因。您可以在某个隐藏字段中添加该值,并在服务器端使用它。

的HTML

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

Java脚本

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;

如何在没有for循环的情况下在Javascript中使用Django模板标签 - javascript

我想在JavaScript中使用模板变量:我的问题是在javascript代码中使用for循环,for循环之间的所有事情都会重复..但我不想要....下面粘贴了我的代码..有人可以告诉我更好的方法吗这..因为这看起来很丑..这是我的代码: {% block extra_javascript %} <script src="/static/js…

使用JS和PHP更改弹出窗口背景图像 - javascript

我有一个JS函数:function zoom(now) { document.getElementById("popup").style.display = "block"; document.getElementById("photos").style.backgroundImage = …

打印二维阵列 - javascript

我正在尝试打印子元素。在this example之后。怎么做?。$myarray = array("DO"=>array('IDEAS','BRANDS','CREATIVE','CAMPAIGNS'), "JOCKEY"=>a…

执行onclick时获得意外令牌 - javascript

我正在使用onclick事件从PHP调用JS函数。这是我的代码:我在一个函数中,因此我需要通过PHP来完成它,因为然后我会返回:$html = '<input type="checkbox" checked value="1" id="setGetSku" name="se…

Javascript到PHP的转换 - javascript

我有一个libphonenumber软件包的javascript端口,它具有以下功能:function cleanPhone(a){ a=a.replace(/[^\d\+]/g,""); return a="+"==a.substr(0,1)?"+"+a.replace(/[^\d]/g,…