The aim of this article is to answer the question 'How do I add items to a DropDownList?'
Initially as part of the declaration for the DropDownList we can also define a number of items, much in the same way as in classic ASP.
<asp:DropdownList id=ddlFrequency runat="server">
<asp:ListItem Text="Daily" Value="Daily" Selected="True"></asp:ListItem>
<asp:ListItem Text="Weekly" Value="Weekly"></asp:ListItem>
<asp:ListItem Text="Monthly" Value="Monthly"></asp:ListItem>
</asp:DropdownList>
Now for the second part of the exercise. In our code behind we wish to be able to add more items to this list. To do this we add the following at the appropriate place in our code
ddlFrequency.Items.Add("Yearly")
However, this only allows us to add a value, we may also wish to add a value and text pair. To do this we need to do the following
ddlYear.Items.Add(New ListItem("1992", "(J/K)"))
To make use of the ListItem it is necessary to add
Imports System.Web.UI.WebControls
at the start of your code.
NAT September 2005 (Revised)
NAT March 2006 (Revised)