How to ADD Item in DropDownList from TextBox on Button Click Event in ASP.Net

Hi, Here in this asp.net tutorials we are going to learn how to add new Item in DropDownList Control from TextBox Control while clicking the Button Control in asp.net with C#.

First we design the ASP.Net web form with one TextBox Control, one DropDownLinst Control and one Button Control.

The HTML Design code for asp.net web form is like:

<table>
<tr>
<td style=”width: 100px; text-align: right”>
Enter Value :</td>
<td style=”width: 100px”>
<asp:TextBox ID=”txtvalue” runat=”server”></asp:TextBox></td>
</tr>
<tr>
<td style=”width: 100px; text-align: right”>
</td>
<td style=”width: 100px; text-align: left”>
<asp:Button ID=”btnadd” runat=”server” Font-Bold=”True” Height=”32px” OnClick=”btnadd_Click”
Text=”ADD” Width=”80px” /></td>
</tr>
<tr>
<td style=”width: 100px; height: 6px; text-align: right”>
Value :</td>
<td style=”width: 100px; height: 6px; text-align: left”>
<asp:DropDownList ID=”Drpvalue” runat=”server” Width=”128px”>
</asp:DropDownList></td>
</tr>
<tr>
<td style=”width: 100px; text-align: right”>
</td>
<td colspan=”1″ style=”text-align: left”>
<asp:Label ID=”Label1″ runat=”server” Font-Bold=”True”></asp:Label></td>
</tr>
</table>

The asp.net web form out put is:

How to ADD Value in DropDownList from TextBox on Button Click Event in ASP.Net

How to ADD Value in DropDownList from TextBox on Button Click Event in ASP.Net

Now, write the below code on Button control click event for add items to DropDownList control from TextBox Control in ASP.Net.

protected void btnadd_Click(object sender, EventArgs e)
{
if (txtvalue.Text == “”)
{
Label1.Text = “Enter Value !!”;
txtvalue.Focus();
}
else
{
Drpvalue.Items.Add(txtvalue.Text);
Label1.Text = “Item Added Successfully !!”;
}
}

The out put of asp.net example of dropdownlist control is:

How to ADD Value in DropDownList from TextBox on Button Click Event in ASP.Net

How to ADD Value in DropDownList from TextBox on Button Click Event in ASP.Net

I hope this asp.net example of add new item in DropDownList from TextBox on Button Click event will help you…..

Leave a Reply

Your email address will not be published. Required fields are marked *

5 + 5 =


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">