Follow by Email

Tuesday, September 10, 2013

add items to dropdown in asp.net from code behind | Add items manually to dropdownlist in asp.net | using listitem in asp.net

hi in this post i will show how to add items into dropdownlist from codebehind using listitem in asp.net

aspx:

  <asp:DropDownList ID="ddltest" runat="server" OnSelectedIndexChanged="ddltest_OnSelectedIndexChanged" AutoPostBack="True"></asp:DropDownList>


aspx.cs:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            ListItem l1 = new ListItem("--Select--", "0");
            ListItem l2 = new ListItem("Mobiles", "1");
            ListItem l3 = new ListItem("Laptops", "2");
            ListItem l4 = new ListItem("Headphones", "3");

            ddltest.Items.Add(l1);
            ddltest.Items.Add(l2);
            ddltest.Items.Add(l3);
            ddltest.Items.Add(l4);
        }
    }