Add items to dropdownlist using TextBox in ASP.NET

September 05, 2015 0 Comments

In this article i will show you how to add items in DropdownList with value. In general way if we add items in the list then you can add only add Text in the list. Like
Dropdownlist1.Items.add(string Text);
But you want to add text with value from Textbox in Dropdownlist then check the below example:


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
        ENTER ITEMS :
        <asp:TextBox ID="TextBox1" runat="server" Width="209px"></asp:TextBox>
        <br />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="ADD ITEMS" />
        <br />
        <asp:DropDownList ID="DropDownList1" runat="server">


Code Behind
 protected void Button1_Click(object sender, EventArgs e)
        if (TextBox1.Text.Trim()!="")
            DropDownList1.Items.Add(new ListItem(TextBox1.Text, TextBox1.Text));

Jacob Lefore

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard. Google