views:

101

answers:

1

How do I check whether a DropDownList is initialized in ASP.NET? I'm not talking about checking for empty strings. The dropdownlist is usually unintialized if there the datasource was not bound to it. So when I try to check the contents with something like this

x = DropDownList.SelectedItem.ToString()

it simply bombs...any suggestions?

A: 

You can check if the list has items:

if(dropDownList.Items.Count > 0) {
  // do it...
} else {
  // not initialized
}

If it's initialized it'll have items, if not it wont throw an error.

Ariel