Geeks With Blogs

News

Microsoft MVP


DZone MVB


Moderator at CodeASP.NET


Quiz Master







free counters
Free counters
Added on January 19,2012


Follow Me @vmsdurano

A bit About Me



Disclaimer
The opinions expressed herein are my own personal opinions and does not represent the opinions of my employers. Nor does it represent the opinion of my dog, because I don’t have one.


Vinz' Blog (ProudMonkey) "Code, Beer and Music ~ my way of being a programmer"

This demo shows on how to add a default Select option in the DropDownList. I decided to write this sample demo because I always encounter this question at the ASP.NET forums.

To make this work then you just need to set the property AppendDataBoundItems to TRUE in the DropDownList.

Here’s an example for adding a Select option declaratively at the mark up.

<asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="true">
    <asp:ListItem Value="-1">Select</asp:ListItem>
</asp:DropDownList>

 

As you can see from above declaration we just simply set the AppendDataBoundItems to True and add the Default Select item in the list.

Here’s an example for adding the Select option at the code behind.

DropDownList1.AppendDataBoundItems = true;
DropDownList1.Items.Add(new ListItem("Select", "-1"));
//Bind Your DropDownList here with data from database
 

As you can see,we  programmatically set the AppendDataBoundItems to true and add the Select ListItem option BEFORE populating the DropDownList with data.

Now here’s another way using the Insert method of DropDownList

//Bind Your DropDownList here with data from database
DropDownList1.Items.Insert(0, new ListItem("Select", "-1"));

The output can be seen below with red mark:

 

Technorati Tags: ,
Posted on Tuesday, February 24, 2009 5:37 PM ASP.NET , Tips&Tricks | Back to top


Comments on this post: Adding Default Select Option in the DropDownList

# re: Adding Default Select Option in the DroDownList
Requesting Gravatar...
Awesome, absolutely awesome. THANKS VERY MUCH
Left by Paul Smith on May 08, 2009 2:53 AM

# re: Adding Default Select Option in the DroDownList
Requesting Gravatar...
Nice. Accurate. Useful. Succinct. Loved it.
Left by Jeff Allen on Jul 30, 2009 10:55 PM

# re: Adding Default Select Option in the DroDownList
Requesting Gravatar...
Thank you, Thank you, Thank you, Thank you, Thank you, Thank you :)
Left by amandititita on Nov 20, 2009 4:06 AM

# re: Adding Default Select Option in the DropDownList
Requesting Gravatar...
Occasionally I find the databound items doubling up, a problem with view state. An alternative is to use a UNION select. See the attached article
Left by dave on May 31, 2011 10:59 PM

comments powered by Disqus

Copyright © Vincent Maverick Durano | Powered by: GeeksWithBlogs.net