Slide 14.3: The ArrayList object (cont.)
Slide 14.5: The SortedList object
Home

The Hashtable Object


The Hashtable object contains items in key/value pairs. The keys are used as indexes, and the values can be searched through their keys. Items are added to the Hashtable with the Add method. A Hashtable object may automatically generate the text and values to the following controls: RadioButtonList, CheckBoxList, DropDownList, and Listbox. To bind data to a RadioButtonList control, the example takes the following steps:
  1. Create a RadioButtonList control (without any ListItem) in a page.
  2. Add the script that builds the list.
  3. Add a sub routine to be executed when the user clicks on an item in the RadioButtonList control.

demo_hashtable_radio1.aspx

Web
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script language="VB" runat="server">
 Sub Page_Load(s As Object, e As EventArgs) Handles Me.Load
  If Not Page.IsPostBack Then
   dim mycountries = New Hashtable
   mycountries.Add( "N", "Norway" )
   mycountries.Add( "S", "Sweden" )
   mycountries.Add( "F", "France" )
   mycountries.Add( "I", "Italy"  )
   rb.DataSource = mycountries
   rb.DataValueField = "Key"
   rb.DataTextField  = "Value"
   rb.DataBind( )
  End If
 End Sub

 Sub displayMessage( s as Object,e As EventArgs )
  lbl1.text = "Your favorite country is: " & _
    rb.SelectedItem.Text
 End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" > 
 <body> 
  <form runat="server">
   <asp:RadioButtonList id="rb" runat="server"
     AutoPostBack="True"
     onSelectedIndexChanged="displayMessage" />
   <asp:label id="lbl1" runat="server" />
  </form>
 </body>
</html>