search this blog [2700+ asp.net c# examples]

Loading...

Add DropDownList list item programmatically in asp.net c#

asp.net DropDownList: how to add list item programmatically
DropDownListAddItem.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DropDownListAddItem.aspx.cs" Inherits="DropDownListAddItem" %>

<!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>asp.net DropDownList: how to add list item programmatically</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h2 style="color:Navy">DropDownList: Add List Item</h2>
        <asp:Label 
            ID="Label1"
            runat="server"
            Font-Size="Medium"
            ForeColor="Green"
            >
        </asp:Label>
        <br />
        <asp:DropDownList 
            ID="DropDiwnList1"
            runat="server"
            BackColor="OrangeRed"
            ForeColor="AliceBlue"
            >
            <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>
        </asp:DropDownList>
        <br /><br />
        <b style="color:Green">Item Text</b>
        <asp:TextBox ID="TextBox1" runat="server">
        </asp:TextBox>
        <br />
        <b style="color:Green">Item Value</b>
        <asp:TextBox ID="TextBox2" runat="server">
        </asp:TextBox>
        <br />
        <asp:Button ID="Button1" runat="server" Text="Add Item" OnClick="Button1_Click" />
    </div>
    </form>
</body>
</html>
DropDownListAddItem.aspx.cs
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)
    {
        if(!this.IsPostBack)
        {
            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();
        DropDiwnList1.Items.Add(li);
        Label1.Text = "Item Added: " +
            TextBox1.Text.ToString() + "[" +
            TextBox2.Text.ToString() + "]";
    }
}










Related asp.net example