This example shows how to load a HashTable manually, and then bind it to both a DropDownList and a Listbox. Although binding methods for both these controls are exactly the same, sometimes it helps to actually see it in action.
If you don’t know what a HashTable is, try thinking of it as a multi-dimensional array.
<html>
<head>
<meta name=”GENERATOR” Content=”ASP Express 2.1″>
<title>Untitled</title>
<script language=”VB” runat=”server”>
Sub Page_Load(Source as Object, E as EventArgs)
if not Page.IsPostBack then
Dim myHash As New Hashtable()
myHash.Add(“David”, 1)
myHash.Add(“Christopher”, 2)
myHash.Add(“Lisa”, 3)
myHash.Add(“Shannon”, 4)
myHash.Add(“James”, 5)
ddl.DataSource = myHash
lb1.DataSource = myHash
ddl.DataTextField = “Key”
lb1.DataTextField = “Key”
ddl.DataValueField = “Value”
lb1.DataValueField = “Value”
ddl.DataBind()
lb1.DataBind()
lb1.selectedIndex=0
end if
End Sub
Sub getDDL(Source as Object, E as EventArgs)
lblDDL.text=”DropDownList Text = ” & ddl.selectedItem.text & “<br>DropDownList Value = ” & ddl.selectedItem.value
End Sub
Sub getLB(Source as Object, E as EventArgs)
lblLB.text=”ListBox Text = ” & lb1.selectedItem.text & “<br>ListBoxValue = ” & lb1.selectedItem.value
End Sub
</script>
</head>
<body>
<Form id=”form1″ runat=”server”>
<div align=”center”>
<table width=”75%”> <tr>
<td align=”center” valign=”Top”>
<asp:DropDownList id=”ddl” runat=”server” /> <p>
<asp:Button id=”button1″ Text=”Get Selected” onclick=”getDDL” runat=”server” /><br>
<asp:Label ID=”lblDDL” runat=”server” />
</td>
<td align=”center” valign=”Top”>
<asp:ListBox id=”lb1″ runat=”server” /><p>
<asp:Button id=”button2″ Text=”Get Selected” onclick=”getLB” runat=”server” /><br>
<asp:Label ID=”lblLB” runat=”server” />
</td>
</tr>
</table></div>
</Form>
</body>