Adding Default Select Option in the DropDownList

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:

DDLSelect

This article is part of the GWB Archives. Original Author: Vincent Maverick Durano

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.