How to add items in DropDownList in C#

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>

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" "">
<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
        <asp:DropDownList ID="ddlCountry" runat="server" AppendDataBoundItems="true">
            <asp:ListItem Text="Select" Value="-1"></asp:ListItem>
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";
    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;

Next Post Previous Post
No Comment
Add Comment
comment url