- How to add an item to a DropDownList programmatically

Add an item to DropDownList programmatically
dropdownlist is an list web server control that allow users to select a single item at a time. developers can add list items to dropdownlist control at design time or programmatically at run time or data bind with a data source control.

the following example code demonstrate us how can we add list item programmatically at run time using c# script section. dropdownlist Items property get the collection of items in the dropdownlist. we can add a ListItem to this items collection using its Add method (ListItemCollection.Add method). this method append a ListItem to the end of collection.

Add method have two overload, they are Add(ListItem) and Add(String). so we need to create a new ListItem object first and then add it to the items collection. ListItem object have three possible properties such as Text, Value and Selected. Text property hold the text that displayed in the list. Value property is hidden and associated with specific item. Selected boolean value indicate whether the item is selected or not.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DropDownListAddItem.aspx.cs" Inherits="DropDownListAddItem" %>

<!DOCTYPE html>

<html xmlns="">
<head runat="server">
    <title> DropDownList: how to add list item programmatically</title>
    <form id="form1" runat="server">
        <h2 style="color:Navy">DropDownList: Add List Item</h2>
        <br />
            <asp:ListItem Text="TreeView" Value="1"></asp:ListItem>
            <asp:ListItem Text="Menu" Value="2"></asp:ListItem>
            <asp:ListItem Text="FormView" Value="3"></asp:ListItem>
            <asp:ListItem Text="DataGrid" Value="4"></asp:ListItem>
        <br /><br />
        <b style="color:Green">Item Text</b>
        <asp:TextBox ID="TextBox1" runat="server">
        <br />
        <b style="color:Green">Item Value</b>
        <asp:TextBox ID="TextBox2" runat="server">
        <br />
        <asp:Button ID="Button1" runat="server" Text="Add Item" OnClick="Button1_Click" />

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class DropDownListAddItem : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
            TextBox1.BackColor = System.Drawing.Color.OrangeRed;
            TextBox1.ForeColor = System.Drawing.Color.Yellow;
            TextBox2.BackColor = System.Drawing.Color.OrangeRed;
            TextBox2.ForeColor = System.Drawing.Color.Yellow;
            Button1.Font.Bold = true;
            Button1.ForeColor = System.Drawing.Color.Green;

    protected void Button1_Click(object sender, EventArgs e) {
        ListItem li = new ListItem();
        li.Text = TextBox1.Text.ToString();
        li.Value = TextBox2.Text.ToString();
        Label1.Text = "Item Added: " +
            TextBox1.Text.ToString() + "[" +
            TextBox2.Text.ToString() + "]";
More examples