将从下拉列表中选择的值获取到字符串变量 c#

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11791319/
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-08-09 19:16:33  来源:igfitidea点击:

Getting the value selected from drop down list into a string variable c#

c#asp.netdrop-down-menu

提问by Plotter

static public String Titl;    

Now in simple programming language suppose i got a dropdown list on my form. when i debugged i found out that dropdowlist with the id dropdownlist1 when written

现在用简单的编程语言假设我的表单上有一个下拉列表。当我调试时,我发现 dropdowlist 的 id 为 dropdownlist1

dropdownlist1.text; 

contains the value i selected in the dropdown menu. However when i did something like this:

包含我在下拉菜单中选择的值。但是,当我做这样的事情时:

titl = dropdownlist1.text;    

i was not getting the value in the titl variable. Help needed. Thank You!

我没有得到 title 变量中的值。需要帮助。谢谢你!

回答by user1102001

check this if you want text..then

如果你想要文本,请检查这个..然后

 title=dropdownlist1.selecteditem.text;

and if you want value then

如果你想要价值,那么

  title=dropdownlist1.selecteditem.value;

回答by Vinoth

 Title = !string.IsNullorEmpty(dropdownlist1.SelectedItem.Text) ?
 dropdownlist1.SelectedITem.Text : "";

回答by Plotter

 Titl = dropdownlist1.SelectedValue;

Works! Thank You!

作品!谢谢你!

回答by Nikhil D

int selectedIndex=dropdownlist1.SelectedIndex;//get index
string selectedText=dropdownlist1.SelectedItem.Text;//get text
string selectedValue=dropdownlist1.SelectedItem.Value;//get value