C# 如何以编程方式按值选择下拉列表项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1249394/
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
How to SELECT a dropdown list item by value programmatically
提问by David Bonnici
How to SELECT a drop down list item by value programatically in C#.NET?
如何在 C#.NET 中以编程方式按值选择下拉列表项?
采纳答案by ScottE
If you know that the dropdownlist contains the value you're looking to select, use:
如果您知道下拉列表包含您要选择的值,请使用:
ddl.SelectedValue = "2";
If you're not sure if the value exists, use (or you'll get a null reference exception):
如果您不确定该值是否存在,请使用(否则您将获得空引用异常):
ListItem selectedListItem = ddl.Items.FindByValue("2");
if (selectedListItem != null)
{
selectedListItem.Selected = true;
}
回答by Henk Holterman
combobox1.SelectedValue = x;
I suspect you may want yo hear something else, but this is what you asked for.
我怀疑您可能希望您听到其他内容,但这正是您要的。
回答by womp
Please try below:
请尝试以下:
myDropDown.SelectedIndex =
myDropDown.Items.IndexOf(myDropDown.Items.FindByValue("myValue"))
回答by Prado
This is a simple way to select an option from a dropdownlist based on a string val
这是一种基于字符串 val 从下拉列表中选择选项的简单方法
private void SetDDLs(DropDownList d,string val)
{
ListItem li;
for (int i = 0; i < d.Items.Count; i++)
{
li = d.Items[i];
if (li.Value == val)
{
d.SelectedIndex = i;
break;
}
}
}
回答by Ian Boyd
ddl.SetSelectedValue("2");
With a handy extension:
有一个方便的扩展:
public static class WebExtensions
{
/// <summary>
/// Selects the item in the list control that contains the specified value, if it exists.
/// </summary>
/// <param name="dropDownList"></param>
/// <param name="selectedValue">The value of the item in the list control to select</param>
/// <returns>Returns true if the value exists in the list control, false otherwise</returns>
public static Boolean SetSelectedValue(this DropDownList dropDownList, String selectedValue)
{
ListItem selectedListItem = dropDownList.Items.FindByValue(selectedValue);
if (selectedListItem != null)
{
selectedListItem.Selected = true;
return true;
}
else
return false;
}
}
Note: Any code is released into the public domain. No attribution required.
注意:任何代码都发布到公共领域。不需要归属。
回答by NoCake
For those who come here by search (because this thread is over 3 years old):
对于那些通过搜索来到这里的人(因为这个线程已经超过 3 年了):
string entry // replace with search value
if (comboBox.Items.Contains(entry))
comboBox.SelectedIndex = comboBox.Items.IndexOf(entry);
else
comboBox.SelectedIndex = 0;
回答by Dan B
Ian Boyd (above) had a great answer -- Add this to Ian Boyd's class "WebExtensions" to select an item in a dropdownlist based on text:
Ian Boyd(上面)有一个很好的答案——将这个添加到 Ian Boyd 的“WebExtensions”类中,以根据文本在下拉列表中选择一个项目:
/// <summary>
/// Selects the item in the list control that contains the specified text, if it exists.
/// </summary>
/// <param name="dropDownList"></param>
/// <param name="selectedText">The text of the item in the list control to select</param>
/// <returns>Returns true if the value exists in the list control, false otherwise</returns>
public static Boolean SetSelectedText(this DropDownList dropDownList, String selectedText)
{
ListItem selectedListItem = dropDownList.Items.FindByText(selectedText);
if (selectedListItem != null)
{
selectedListItem.Selected = true;
return true;
}
else
return false;
}
To call it:
调用它:
WebExtensions.SetSelectedText(MyDropDownList, "MyValue");
回答by San
I prefer
我更喜欢
if(ddl.Items.FindByValue(string) != null)
{
ddl.Items.FindByValue(string).Selected = true;
}
Replace ddl with the dropdownlist ID and string with your string variable name or value.
将 ddl 替换为下拉列表 ID,并将字符串替换为您的字符串变量名称或值。
回答by Ankursonikajen
ddlPageSize.Items.FindByValue("25").Selected = true;
ddlPageSize.Items.FindByValue("25").Selected = true;
回答by Shahriar Rahman Zahin
If anyone else is trying this and facing problem then let me point one possible problem: If you are using Web Application then inside Page_Loadif you are loading drop-down from DB and at the sametime you want to load data, then first load your drop-down lists and then load your data with selected drop-down conditions.
如果其他人正在尝试此操作并面临问题,那么让我指出一个可能的问题:如果您使用的是 Web 应用程序,则在Page_Load 中,如果您正在从 DB 加载下拉列表,同时又想加载数据,则首先加载您的下拉列表-down 列表,然后使用选定的下拉条件加载您的数据。
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
LoadDropdown(); //drop-downs generated first
LoadData(); // other data loading and drop-down value selection logic here
}
}
And for successfully selecting drop-down from code follow the approved answer above which is
为了成功地从代码中选择下拉菜单,请遵循上面批准的答案
ddl.SelectedValue = "2";
.
ddl.SelectedValue = "2";
.
Hope it solves the problem
希望能解决问题