Friday, May 13, 2005

Selecting Items in DropDownList

This morning, I found an ineresting behavior with the DropDownList. Take the following two lines of code, which appear to do the same thing on the surface, but in reality do two completely different things.

ddl.Items.FindByValue(value).Selected = true;
ddl.SelectedValue = value;


The second one works as expected. The first one though results in two items being selected in the DropDownList.

Moral of the story:
When you know the value of the item in a DropDownList that you want to select, use the SelectedValue property to set the selected item in a DropDownList.

No comments: