CODEDIGEST
Home Articles CodeDigest Tutorials FAQs
Skip Navigation LinksHome » CodeDigest » DropDownList control with "Select" as First Item   You are not logged in.
Search
 

Sponsored Links
 

Technologies
 

DropDownList control with "Select" as First Item
Submitted By Satheesh Babu B
On 10/29/2008 7:06:21 AM
Tags: asp.net,CodeDigest  

 

Whenever we use DropDownList control, we will have the first item as "Select" or something similar to notify the user to select an item.

This can be achieved in two ways. In codebehind and in ASPX.

 

Through CodeBehind

protected void Page_Load(object sender, EventArgs e)

    {

 

if (!IsPostBack)

        {

         ddlRoles.DataSource = GetAllRoles();

 

         ddlRoles.DataValueField = "RoleID";

 

         ddlRoles.DataTextField = "Role";

 

         ddlRoles.DataBind();

 

         ddlRoles.Items.Insert(0, new ListItem("Select",""));

        }

}

 

In ASPX

 

        <asp:DropDownList ID="ddlRank" AppendDataBoundItems="true" runat="server">

         <asp:ListItem Text="Select" Value=""></asp:ListItem>

        </asp:DropDownList>

 

By setting AppendDataBoundItems property to true and specifying a default item in the markup will make the dropdownlist to have the first item as "Select". Setting this property will make sure that all items added henceforth in codebehind will be appended to the item collection.

 

Execute the page. You will receive an error message when you click Save without selecting a value in DropDownList control.

Do you have a working code that can be used by anyone? Submit it here. It may help someone in the community!!

Recent Codes
  • View All Codes..