gpt4 book ai didi

c# - 使用 Javascript 设置下拉列表的值并在 C# 中获取该值

转载 作者:行者123 更新时间:2023-11-28 01:35:19 26 4
gpt4 key购买 nike

    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服务器端的原因代码。您可以在某些隐藏字段中添加值并在服务器端使用该值。

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;

关于c# - 使用 Javascript 设置下拉列表的值并在 C# 中获取该值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21603167/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com