aspdotnet-suresh offers articles and tutorials,csharp dot net, articles and tutorials,VB.NET Articles,Gridview articles,code examples of 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

ASP.Net - Add Items to Dropdownlist Programmatically on Button Click in C# and VB.Net

Oct 18, 2014

Here I will explain how to add items to dropdownlist programmatically on button click in using c# and or dynamically add items to dropdownlist on button click in using c# and

In previous articles I explained Cascading dropdownlist example in, highlight gridview records based on search in, filter gridview records based on dropdown selection in and many articles relating to, c#, and JQuery. Now I will explain how to add items to dropdownlist programmatically in using c# and

To implement this first create new application and write the code in aspx page like as shown below

<html xmlns="">
<head id="Head1" runat="server">
<title>Dynamically Add items to dropdownlist in</title>
<form id="form1" runat="server">
<b>Enter Name</b><asp:TextBox ID="txtName" runat="server" /><asp:Button ID="btnAdd"
runat="server" Text="Add Items" onclick="btnAdd_Click" /><br />
Select UserName: <asp:DropDownList ID="ddlUsername" runat="server"/>
Now open code behind file and write the following code

C# Code

using System;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
    protected void btnAdd_Click(object sender, EventArgs e)
        if (txtName.Text.Trim() != "")
            ddlUsername.Items.Add(new ListItem(txtName.Text, txtName.Text));


Partial Class VbCode
    Inherits System.Web.UI.Page
    Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As EventArgs)
        If txtName.Text.Trim() <> "" Then
            ddlUsername.Items.Add(New ListItem(txtName.Text, txtName.Text))
        End If
    End Sub
End Class


If you enjoyed this post, please support the blog below. It's FREE!

Get the latest,, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email


Give your Valuable Comments

Other Related Posts

© 2015 All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.