Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

ASP.NET Hashtable


May 12, 2021 ASP.NET


Table of contents


ASP.NET Web Forms - Hashtable object

This section describes using ASP.NET to bind data using the Hashtable object.

The Hashtable object contains items represented by key/value pairs.


ASP.NET Hashtable

Try it - instance

Hashtable RadiobuttonList 1

Hashtable RadiobuttonList 2

Hashtable DropDownList


Create Hashtable

The Hashtable object contains items represented by key/value pairs. Keys are used as indexes to enable a quick search of values by searching for keys.

Add an item to Hashtable using the Add() method.

The following code creates a Hashtable object called mycountries and adds four elements:

<script runat="server">
Sub Page_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")
end if
end sub
</script>


Data binding

Hashtable objects automatically generate text and values for the following controls:

  • asp:RadioButtonList
  • asp:CheckBoxList
  • asp:DropDownList
  • asp:Listbox

In order to bind data to the RadioButtonList control, first create a RadioButtonList control (without any asp:ListItem elements) in the .aspx page:

<html>
<body>

<form runat="server">
<asp:RadioButtonList id="rb" runat="server" AutoPostBack="True" />
</form>

</body>
</html>

Then add the script that created the list, and bind the values in the list to the RadioButtonList control:

<script runat="server">
sub Page_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
</script>

<html>
<body>

<form runat="server">
<asp:RadioButtonList id="rb" runat="server" AutoPostBack="True" />
</form>

</body>
</html>

Then we add a sub-routine that is executed when the user clicks on an item in the RadioButtonList control. When a single button is clicked, a line of text appears in the label:

<script runat="server">
sub Page_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>
<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>

Demo Examples . . .

Note: You cannot choose how items added to Hashtable are sorted. To alphabetically or numerically sort items, use the SortedList object.