Different Ways to Bind or Populate Dropdownlist Control In Asp.Net

How to Bind or Populate Asp.Net Dropdownlist Control

November 21, 2020

In this article we will cover different ways to Bind or Populate DropDownList Control in Asp.Net

<asp:DropDownList ID="ddlCountry" runat="server"></asp:DropDownList>

Example 1

List<Countrycountries = new List<Country>()
{
    new Country(){ Name = "India",Code = "IN"},
    new Country(){ Name = "USA",Code = "US"}
};
 
ddlCountry.DataSource = countries;
ddlCountry.DataTextField = "Name";
ddlCountry.DataValueField = "Code";
ddlCountry.DataBind();
ddlCountry.Items.Insert(0, new ListItem() { Text = "Please Select", Value = "" });

Example 2

ddlCountry.Items.Add(new ListItem() { Text = "Please Select", Value = "" });
ddlCountry.Items.Add(new ListItem() { Text = "India", Value = "IN" });
ddlCountry.Items.Add(new ListItem() { Text = "USA", Value = "US" });

Example 3

(Use this method when items needs to be added on perticular index)
ddlCountry.Items.Insert(0, new ListItem() { Text = "Please Select", Value = "" });
ddlCountry.Items.Insert(1, new ListItem() { Text = "India", Value = "IN" });
ddlCountry.Items.Insert(2, new ListItem() { Text = "USA", Value = "US" });

Post Comments(0)

Leave a reply

Will not be displayed in comment box .

Loading...