gpt4 book ai didi

c# - 如何从后面的 asp.net 代码中的 html 组合框获取值

转载 作者:行者123 更新时间:2023-11-28 03:02:12 25 4
gpt4 key购买 nike

我无法从 asp.net 的组合框中获取选定的值。我通过添加 id 和 runat="server"并使用 bootstrap 属性在服务器端编码中使用 HTML 组合框。如何在单击按钮时获取组合框的值

<div class="form-group">
<label class="col-xs-3 control-label">Size</label>
<div class="col-xs-5 selectContainer">

<select class="form-control" name="size" runat="server" id="Combobox">
<option value="">Choose a size</option>
<option value="s">Small (S)</option>
<option value="m">Medium (M)</option>
<option value="l">Large (L)</option>
<option value="xl">Extra large (XL)</option>
</select>
</div>
</div>

code behind


string value = Combobox.Items.ToString();

最佳答案

在代码隐藏中,带有 runat=server 的 html-select 是一个 HtmlSelectItems返回 ListItemCollection .

您可以使用 SelectedIndex :

string selectedItemValue = null;
string selectedItemText = null;
if(Combobox.SelectedIndex >= 0)
{
ListItem selectedItem = Combobox.Items[Combobox.SelectedIndex];
selectedItemValue = selectedItem.Value;
selectedItemText = selectedItem.Text;
}

或使用 HtmlSelect.Value具有相同功能的属性:

string selectedItemValue = Combobox.Value; // String.Empty if no item selected

关于c# - 如何从后面的 asp.net 代码中的 html 组合框获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34308232/

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