Sunday, December 10, 2006

Select ListControl Value

The SelectedValue property of a ListControl can be used to select an item in the list.  However, if no items in the list control contain the specified value, an ArgumentOutOfRangeException is thrown.  This exception can be avoided by using the IndexOf and FindByValue methods of the ListItemCollection.

control.SelectedIndex = control.Items.IndexOf(control.Items.FindByValue(value));

The IndexOf method returns -1 if the specified item is not found and null is returned by the FindByValue method.

No comments: