DropDownList control is similar to a RadioButtonList control with less screen read state. We can add options to a DropDownList in many ways: we can add items directly to the ListItemCollection collection of the DropDownList control, we can bind a DropDownList control to a dataSource, or we can also bind a DropDownList control at the time of declaration.
If you want to add items at the time of declaration:
<asp:DropDownList ID="DropDownList1" runat="server"> <asp:ListItem Text="USA"/> <asp:ListItem Text="India"/> <asp:ListItem Text="Japan"/> </asp:DropDownList>
And if you want to add List items directly to the ListItemCollection collection of the DropDownList control, for example:
<%@ Page Language="VB" AutoEventWireup="true" CodeFile="Default.aspx.vb" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> Sub Page_Load() If Not IsPostBack Then DropDownList1.Items.Add("USA") DropDownList1.Items.Add("India") DropDownList1.Items.Add("Japan") End If End Sub </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title>bind DropDownList control</title> </head> <body style="height: 410px; width: 768px"> <form runat="server"> <asp:DropDownList ID="DropDownList1" runat="server"> </asp:DropDownList> </form> </body> </html>
And in the last if you want to bind control to a DataSource, for example:
In the following example first we create a arraylist and then we assign this arraylist to datasource property of DropDownList control
<%@ Page Language="VB" AutoEventWireup="true" CodeFile="Default.aspx.vb" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> Sub Page_Load() Dim arrList As New ArrayList arrList.Add("USA") arrList.Add("India") arrList.Add("Japan") DropDownList1.DataSource = arrList DropDownList1.DataBind() End Sub </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title>bind DropDownList control</title> </head> <body style="height: 410px; width: 768px"> <form runat="server"> <asp:DropDownList ID="DropDownList1" runat="server"> </asp:DropDownList> </form> </body> </html>