vb.net 按选定文本获取下拉列表选定值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22801251/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 17:28:43  来源:igfitidea点击:

Get DropDownList Selected Value by Selected Text

vb.netdrop-down-menu

提问by eric

Do anyone knows how to get selected value of a drop down list by selected text?

有谁知道如何通过选定的文本获取下拉列表的选定值?

I tried the below method but doesn't work:

我尝试了以下方法但不起作用:

ddlWorkType.SelectedItem.Text = "writing"
myddlvalue = ddlWorkType.SelectedValue

Please show me how. Thanks.

请告诉我怎么做。谢谢。

回答by sk2185

 DDLResp.Items.FindByText(TxtResp.Text.Trim).Selected = True

Try this Code

试试这个代码

回答by Rahul Shinde

how to get selected value and selected text in vb.net

如何在 vb.net 中获取选定的值和选定的文本

<asp:DropDownList CssClass="textbox" ID="ddlUser"  runat="server">
<asp:ListItem Value="0">Select User</asp:ListItem>
</asp:DropDownList><br />

Selected DropDownList value

选定的 DropDownList 值

ddlvalue = ddlUser.SelectedItem.Value

Selected DropDownList text

选定的下拉列表文本

ddlText = ddlUser.SelectedItem.Text

回答by Shell

Selected text is actually text which you selecting through mouse drag or by press shift key to copy/paste or delete text. You need to use just .Textinstead of .SelectedText

所选文本实际上是您通过鼠标拖动或按 shift 键复制/粘贴或删除文本选择的文本。你需要使用 just.Text而不是.SelectedText

ddlWorkType.Text = "writing"
myddlvalue = ddlWorkType.SelectedValue

if the text does not exist in assign datasource then combobox SelectedIndex will return -1 and SelectedValue will return Nothing.

如果文本在分配数据源中不存在,则组合框 SelectedIndex 将返回 -1,SelectedValue 将返回 Nothing。