How to add an item to a drop down list in ASP.NET? How to add an item to a drop down list in ASP.NET? asp.net asp.net

How to add an item to a drop down list in ASP.NET?


Try this, it will insert the list item at index 0;

DropDownList1.Items.Insert(0, new ListItem("Add New", ""));


Which specific index? If you want 'Add New' to be first on the dropdownlist you can add it though the code like this:

<asp:DropDownList ID="DropDownList1" AppendDataBoundItems="true" runat="server">     <asp:ListItem Text="Add New" Value="0" /></asp:DropDownList>

If you want to add it at a different index, maybe the last then try:

ListItem lst = new ListItem ( "Add New" , "0" );DropDownList1.Items.Insert( DropDownList1.Items.Count-1 ,lst);


Try following code;

DropDownList1.Items.Add(new ListItem(txt_box1.Text));