In this short article I am providing a code snippet to programmatically add item to ASP.Net DropDownList from code behind using TextBox and a Button.
The idea is to dynamically add an item to DropDownList control with the value entered in the TextBox on Button click in ASP.Net.
 
HTML Markup
The HTML Markup consists of TextBox, Button and a DropDownList.
<asp:TextBox ID="txtFruit" runat="server" />
<asp:Button ID="btnAdd" runat="server" Text="Add Item" OnClick="AddItem" />
<br />
<br />
<asp:DropDownList ID="ddlFruits" runat="server">
    <asp:ListItem Text="----Fruits----" Value=""></asp:ListItem>
</asp:DropDownList>
 
 
Programmatically adding item to ASP.Net DropDownList control from code behind
Below is the Button click event handler, when the Button is clicked the name of the Fruit entered in the TextBox is stored in a string variable.
Then if the value is not NULL, it is added to the DropDownList using the DropDownList Items Add method which accepts object of type ListItem class.
C#
protected void AddItem(object sender, EventArgs e)
{
    string fruit = txtFruit.Text.Trim();
    if (!string.IsNullOrEmpty(fruit))
    {
        ddlFruits.Items.Add(new ListItem(fruit, fruit));
    }
}
 
VB.Net
Protected Sub AddItem(sender As Object, e As EventArgs)
    Dim fruit As String = txtFruit.Text.Trim()
    If Not String.IsNullOrEmpty(fruit) Then
        ddlFruits.Items.Add(New ListItem(fruit, fruit))
    End If
End Sub
 
Programmatically add items to DropDownList on Button Click in ASP.Net using C# and VB.Net
 
Demo
 
Downloads