Best way to check if dropdown list contains value?

When a user navigates to a new page, this selected ddl index is determined by the cookie, but if ddl does not contain this cookie value, I would like it to be set to 0. What method would I like to use? use for ddl? Is the loop the best, or is there just an if statement that I can execute?

This is what I tried, but it does not return a bool.

if ( !ddlCustomerNumber.Items.FindByText( GetCustomerNumberCookie().ToString() ) ) ddlCustomerNumber.SelectedIndex = 0; 
+53
c # drop-down-menu
Jan 05
source share
10 answers

Two methods come to mind:

You can use Contains like this:

 if (ddlCustomerNumber.Items.Contains(new ListItem(GetCustomerNumberCookie().ToString()))) { // ... code here } 

or change the current strategy:

 if (ddlCustomerNumber.Items.FindByText( GetCustomerNumberCookie().ToString()) != null) { // ... code here } 

EDIT: There is also DropDownList.Items.FindByValue , which works the same as FindByText, except that it searches based on values.

+117
Jan 05 '10 at 16:03
source share

This will return the item. Just change to:

 if (ddlCustomerNumber.Items.FindByText( GetCustomerNumberCookie().ToString()) != null) ddlCustomerNumber.SelectedIndex = 0; 
+9
Jan 05 '10 at 16:01
source share

If the value 0 is your default value, you can simply use a simple assignment:

 ddlCustomerNumber.SelectedValue = GetCustomerNumberCookie().ToString(); 

This automatically selects the correct list item if the DDL contains a cookie value. If it does not contain it, this call will not change the selection, so it will remain at the selected by default. If the latter is the same as the value 0, then this is the perfect solution for you.

I use this mechanism quite a lot and find it very convenient.

+5
Mar 18 '13 at 15:32
source share

How about this:

 ListItem match = ddlCustomerNumber.Items.FindByText( GetCustomerNumberCookie().ToString()); if (match == null) ddlCustomerNumber.SelectedIndex = 0; //else // match.Selected = true; // you'll probably select that cookie value 
+1
Jan 05 '10 at 16:04
source share
 ListItem item = ddlComputedliat1.Items.FindByText("Amt D"); if (item == null) { ddlComputedliat1.Items.Insert(1, lblnewamountamt.Text); } 
+1
Jun 02 '16 at 11:07 on
source share

You can try to check if this method returns null:

 if (ddlCustomerNumber.Items.FindByText(GetCustomerNumberCookie().ToString()) != null) ddlCustomerNumber.SelectedIndex = 0; 
0
Jan 05 '10 at 16:03
source share

In C #, this works:

  if (DDLAlmacen.Items.Count > 0) { if (DDLAlmacen.Items.FindByValue("AlmacenDefectoAndes").Value == "AlmacenDefectoAndes") { DDLAlmacen.SelectedValue = "AlmacenDefectoAndes"; } } 

Update:

Translation of the code above on Visual Basic does not work. It throws a "System.NullReferenceException: the object reference is not set to the object instance .."

So. for this, to work with Visual Basic, I had to change the code as follows:

  If DDLAlmacen.Items.Count > 0 Then If DDLAlmacen.Items.Contains(New ListItem("AlmacenDefectoAndes")) Then DDLAlmacen.SelectedValue = "AlmacenDefectoAndes" End If End If 
0
May 26 '12 at 16:50
source share

//you can use? operator instead of if

 ddlCustomerNumber.SelectedValue = ddlType.Items.FindByValue(GetCustomerNumberCookie().ToString()) != null ? GetCustomerNumberCookie().ToString() : "0"; 
0
Jan 18 '16 at 15:10
source share

If the function returns nothing, you can try this below

 if (ddlCustomerNumber.Items.FindByText( GetCustomerNumberCookie().ToString()) != Nothing) { ... } 
0
Jan 22 '18 at 5:46
source share

Sometimes a value needs to be truncated from a space or it will not match, in which case you can use this additional step ( source ):

 if(((DropDownList) myControl1).Items.Cast<ListItem>().Select(i => i.Value.Trim() == ctrl.value.Trim()).FirstOrDefault() != null){} 
0
May 23 '19 at 16:00
source share



All Articles