Web Pages Tutor...login
Web Pages Tutorial
author:php.cn  update time:2022-04-11 14:20:28

Web Forms Hashtable


ASP.NET Web Forms - Hashtable Object


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



Try it - Example

Hashtable RadiobuttonList 1

Hashtable RadiobuttonList 2

Hashtable DropDownList


Creating a Hashtable

A Hashtable object contains items represented by key/value pairs. The key is used as an index, and by searching for the key, a quick search of the value can be achieved.

Add items to the Hashtable through the Add() method.

The following code creates a Hashtable object named 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 can 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 in the .aspx page (without any asp:ListItem elements):

<html>
<body>

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

</body>
</html>

Then add the script that creates 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 subexample This subroutine will be executed when the user clicks an item in the RadioButtonList control. When a radio button is clicked, a line of text will appear in the label:

Instance

<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>

<!DOCTYPE html>
<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>

Run Example»

Click the "Run Instance" button to view the online instance

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


php.cn