How to select a program item from the dropdown list by value

How to select a dropdown list item by value programmatically in C # .NET?

+43
c # drop-down-menu
Aug 08 '09 at 17:15
source share
10 answers

If you know that the drop-down list contains the value you want to select, use:

ddl.SelectedValue = "2"; 

If you are not sure if this value exists, use (or you will get an exception with a null reference):

 ListItem selectedListItem = ddl.Items.FindByValue("2"); if (selectedListItem != null) { selectedListItem.Selected = true; } 
+78
Aug 08 '09 at 17:44
source share

Please try below:

 myDropDown.SelectedIndex = myDropDown.Items.IndexOf(myDropDown.Items.FindByValue("myValue")) 
+24
Aug 08 '09 at 17:23
source share
 ddl.SetSelectedValue("2"); 

With a convenient 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 issued in a public domain. No attribution required.

+6
Jun 28 '12 at 21:14
source share
 combobox1.SelectedValue = x; 

I suspect that you may need to hear something else, but this is what you asked for.

+1
Aug 08 '09 at 17:21
source share

This is an easy way to select an option from the dropdown list based on the string 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; } } } 
+1
Apr 21 2018-11-21T00:
source share

Ian Boyd (above) had a great answer - add this to Ian Boyd’s WebExtensions class to select an item from the drop-down list 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"); 
+1
Feb 20 '14 at 19:37
source share

For those who come here by search (because this topic is older than 3 years):

 string entry // replace with search value if (comboBox.Items.Contains(entry)) comboBox.SelectedIndex = comboBox.Items.IndexOf(entry); else comboBox.SelectedIndex = 0; 
0
Mar 25 '13 at 21:54
source share
0
Nov 07 '13 at 2:24 on
source share

I prefer

 if(ddl.Items.FindByValue(string) != null) { ddl.Items.FindByValue(string).Selected = true; } 

Replace ddl with the drop-down list identifier and a string with the name or value of a string variable.

0
Feb 15 '17 at 15:10
source share

ddlPageSize.Items.FindByValue ("25"). Selected = true;

0
Jun 18 '18 at 11:11
source share



All Articles