When the user navigates to a new page, this ddl's selected index is determined by a cookie, but if the ddl doesn't contain that cookie's value, then I'd like it to be set the 0. What method would I use for the ddl? Is a loop the best way, or is there a simply if statement I can perform?
This is what I've attempted, but it doesn't return a bool.
if ( !ddlCustomerNumber.Items.FindByText( GetCustomerNumberCookie().ToString() ) )
ddlCustomerNumber.SelectedIndex = 0;
What about this:
You could try checking to see if this method returns a null:
There are two methods that come to mind:
You could use Contains like so:
or modifying your current strategy:
EDIT: There's also a
DropDownList.Items.FindByValue
that works the same way as FindByText, except it searches based on values instead.If the function return Nothing, you can try this below
If 0 is your default value, you can just use a simple assignment:
This automatically selects the proper list item, if the DDL contains the value of the cookie. If it doesn't contain it, this call won't change the selection, so it stays at the default selection. If the latter one is the same as value 0, then it's the perfect solution for you.
I use this mechanism quite a lot and find it very handy.