In this post I will show how to add the static item to the dropdownlist like a dropdown that displays one static item such as “select” while the rest of the items are data-bound, then checks out the code below:
Set the AppendDataBoundItems property of the dropdownlist to True, and add in the static list item. Your dropdown will look something like this:
<asp:DropDownList ID="ddlCountry" runat="server" AppendDataBoundItems="true">
<asp:ListItem Text="Select" Value="-1"></asp:ListItem>
</asp:DropDownList>
Check out the complete code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="ddlCountry" runat="server" AppendDataBoundItems="true">
<asp:ListItem Text="Select" Value="-1"></asp:ListItem>
</asp:DropDownList>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ddlCountry.DataSource = new Country().Get();
ddlCountry.DataTextField = "CountryName";
ddlCountry.DataValueField = "ID";
ddlCountry.DataBind();
}
}
public class Country
{
public int ID { get; set; }
public string CountryName { get; set; }
public List<Country> Get()
{
List<Country> list = new List<Country>()
{
new Country(){ID=1,CountryName="India"},
new Country(){ID=2,CountryName="USA"},
new Country(){ID=3,CountryName="UK"},
new Country(){ID=4,CountryName="Denmark"}
};
return list;
}
}
}