Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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!

  • *Experts*
Posted

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

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...