[英]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
但沒有更改DropDownList
的ViewState
,這就是為什么您沒有獲取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.