Hi landomarossi,
Please refer below sample.
HTML
<asp:TextBox ID="txtFruits" runat="server"></asp:TextBox>
<asp:Button ID="btnAdd" runat="server" Text="Add Item" OnClick="AddItem" /><br />
<asp:DropDownList ID="ddlFruits" runat="server">
</asp:DropDownList>
Namespace
C#
using System.Data;
VB.Net
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[1] { new DataColumn("Items") });
dt.Rows.Add("Orange");
dt.Rows.Add("Banana");
dt.Rows.Add("Apple");
ddlFruits.DataTextField = "Items";
ddlFruits.DataSource = dt;
ddlFruits.DataBind();
}
}
protected void AddItem(object sender, EventArgs e)
{
string fruit = txtFruits.Text.Trim();
txtFruits.Text = string.Empty;
if (!string.IsNullOrEmpty(fruit))
{
ddlFruits.Items.Add(new ListItem(fruit, fruit));
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(0) {New DataColumn("Items")})
dt.Rows.Add("Orange")
dt.Rows.Add("Banana")
dt.Rows.Add("Apple")
ddlFruits.DataTextField = "Items"
ddlFruits.DataSource = dt
ddlFruits.DataBind()
End If
End Sub
Protected Sub AddItem(ByVal sender As Object, ByVal e As EventArgs)
Dim fruit As String = txtFruits.Text.Trim()
txtFruits.Text = String.Empty
If Not String.IsNullOrEmpty(fruit) Then
ddlFruits.Items.Add(New ListItem(fruit, fruit))
End If
End Sub
Screenshot