Using Hash Table object in asp.net
The Hashtable object contains items in key/value pairs. The keys are used as indexes, and very quick searches can be made for values by searching through their keys.
Items are added to the Hashtable with the Add() method.
The following code creates a Hashtable named books and three elements are added:
Hashtable books=New Hashtable
books.Add(”b1″,”book1″)
books.Add(”b2″,”book2″)
books.Add(”b3″,”book3″)
Data Binding
A Hashtable object may automatically generate the text and values to the following controls:
asp:RadioButtonList
asp:CheckBoxList
asp:DropDownList
asp:Listbox
To bind data to a RadioButtonList control, first create a RadioButtonList control (without any asp:ListItem elements) in an .aspx page:
<html>
<body><form runat=”server”>
<asp:RadioButtonList id=”rb” runat=”server”
AutoPostBack=”True” /></form></body>
</html>
Then add the code that builds the list:
protected void Page_Load(object sender, EventArgs e)
{
Hashtable books=New Hashtable
books.Add(”b1″,”book1″)
books.Add(”b2″,”book2″)
books.Add(”b3″,”book3″)
rb.DataSource=books
rb.DataValueField=”Key”
rb.DataTextField=”Value”
rb.DataBind()
}
Then we add a function to be executed when the user clicks on an item in the RadioButtonList control. When a radio button is clicked, a text will appear in a label:
Public void displayMessage()
{
lbl1.text=”Your favorite country is: ” + rb.SelectedItem.Text
}
</script>
<html>
<body>
<form runat=”server”>
<asp:RadioButtonList id=”rb” runat=”server”
AutoPostBack=”True” onSelectedIndexChanged=”displayMessage” />
<p><asp:label id=”lbl1″ runat=”server” /></p>
</form>
</body>
</html>
