Create hashtable
The hashtable object contains the items represented by key/value pairs. The key is used as an index. By searching its key, you can quickly search for the value.
Use the add () method to add a project to hashtable.
The following code creates a hashtable named mycountries and adds four elements:
Code
<script runat="server">
Sub Page_Load
if Not Page.IsPostBack then
dim mycountries=New Hashtable
mycountries.Add("C","China")
mycountries.Add("S","Sweden")
mycountries.Add("F","France")
mycountries.Add("I","Italy")
end if
end sub
</script>
Data Binding
The hashtable object can automatically generate text and values for the following controls:
- ASP: radiobuttonlist
- ASP: checkboxlist
- ASP: dropdownlist
- ASP: ListBox
To bind data to a radiobuttonlist control, create a radiobuttonlist control on the. ASPX page (without any ASP: listitem element)
Code
<body>
<form runat="server">
<asp:RadioButtonList id="rb" runat="server" AutoPostBack="True" />
</form>
</body>
Then add the build list script:
Code
<script runat="server">
sub Page_Load
if Not Page.IsPostBack then
dim mycountries=New Hashtable
mycountries.Add("C","China")
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>
<body>
<form runat="server">
<asp:RadioButtonList id="rb" runat="server" AutoPostBack="True" />
</form>
</body>
Then we add a subroutine that will be executed when you click a project in the radiobuttonlist control. When a single-choice button is clicked, a text will appear in the label.
Code
<script runat="server">
sub Page_Load
if Not Page.IsPostBack then
dim mycountries=New Hashtable
mycountries.Add("C","China")
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>
<body>
<form runat="server">
<asp:RadioButtonList id="rb" runat="server"
AutoPostBack="True" onSelectedIndexChanged="displayMessage" />
<p><asp:label id="lbl1" runat="server" /></p>
</form>
</body>