How to SELECT a drop down list item by value programatically in C#.NET?
-
are looking for a way to do this using automation (say for testing)?Steven– Steven2009-08-08 17:18:06 +00:00Commented Aug 8, 2009 at 17:18
-
1Is this for WinForms, WPF, web?Eric J.– Eric J.2009-08-08 17:23:14 +00:00Commented Aug 8, 2009 at 17:23
-
No Im just to select the country by value depending the value I have in the DBDavid Bonnici– David Bonnici2009-08-08 17:28:55 +00:00Commented Aug 8, 2009 at 17:28
-
Possible duplicate of Setting dropdownlist selecteditem programmaticallyMichael Freidgeim– Michael Freidgeim2017-01-16 07:34:26 +00:00Commented Jan 16, 2017 at 7:34
10 Answers
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;
}
2 Comments
Please try below:
myDropDown.SelectedIndex =
myDropDown.Items.IndexOf(myDropDown.Items.FindByValue("myValue"))
3 Comments
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.
Comments
ddlPageSize.Items.FindByValue("25").Selected = true;
1 Comment
combobox1.SelectedValue = x;
I suspect you may want yo hear something else, but this is what you asked for.
2 Comments
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.
2 Comments
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:
/// <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");
Comments
For those who come here by search (because this thread is over 3 years old):
string entry // replace with search value
if (comboBox.Items.Contains(entry))
comboBox.SelectedIndex = comboBox.Items.IndexOf(entry);
else
comboBox.SelectedIndex = 0;
1 Comment
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_Load if 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.
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";.
Hope it solves the problem