DropDownList SelectedIndex Problem

session101

Newcomer
Joined
May 10, 2006
Messages
23
After I add item to a DropDownList, .NET automatically selects the first item for me. How do I default a blank/No Value for this DropDownList?

I tried to make the selectedindex = -1, but it does not seem to work using C# as the language for ASP.NET. Please advise. Thanks!
 
You'll have to add a blank value to your dropdownlist. This is typically the first entry, but doesn't have to be - you can set the SelectedValue or SelectedIndex to match the blank.

If you're using ASP.NET 2.0 and binding your dropdown then you can take advantage of the new AppendDataBoundItems property. In the page you can specify one blank entry with:
<asp:ListItem Value="" Text=""/>

Then on the dropdownlist itself, set AppendDataBoundItems to true. When you add the code to bind the dropdownlist, the new items will be appended after the blank value.

If you're using ASP.NET 1.x, I've traditionally added the blank to my datasource when doing binding.

If you're not binding, then just add the blank :)

-ner
 
Back
Top