您的位置:首页 > 移动开发 > Objective-C

ASP.NET - The Hashtable Object

2007-01-19 17:13 393 查看
The Hashtable//散列表 object//对像 contains items in key/value pairs//对.

Examples

Example 1 - Hashtable RadioButtonList//单选按钮列表

ASPX Source:

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

Output Result:

Norway
Italy
Sweden
France
if you click the radio "Norway",it will shows:

Norway
Italy
Sweden
France
Your favorite country is: Norway

Example 2 - Hashtable RadiobuttonList

ASPX Source:

<script runat="server">
sub Page_Load
if Not Page.IsPostBack then
dim navigate=New Hashtable
navigate.Add("RadioButtonList","control_radiobuttonlist.asp")
navigate.Add("CheckBoxList","control_checkboxlist.asp")
navigate.Add("DropDownList","control_dropdownlist.asp")
navigate.Add("ListBox","control_listbox.asp")
rb.DataSource=navigate
rb.DataValueField="Value"
rb.DataTextField="Key"
rb.DataBind()
end if
end sub

sub navigate(s as Object, e As EventArgs)
response.redirect(rb.SelectedItem.Value)
end sub
</script>

<html> <body>

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

</body> </html>

Output Result:

DropDownList
RadioButtonList
CheckBoxList
ListBox

if you click the radio "DropDownList",It will show:

it will open the file "control_dropdownlist.asp" in the frame of Output Result.
Example 3

- Hashtable DropDownList//下拉菜单式列表

ASPX Source:

<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")
dd.DataSource=mycountries
dd.DataValueField="Key"
dd.DataTextField="Value"
dd.DataBind()
end if
end sub

sub displayMessage(s as Object,e As EventArgs)
lbl1.text="Your favorite country is: " & dd.SelectedItem.Text
end sub
</script>

<html> <body>

<form runat="server">
<asp:DropDownList id="dd" runat="server"
AutoPostBack="True" onSelectedIndexChanged="displayMessage" />
<p><asp:label id="lbl1" runat="server" /></p>
</form>

</body> </html>

Output Result:

Norway
Italy
Sweden
France



it you click the dropDownList "Italy"

Norway
Italy
Sweden
France

Your favorite country is: Italy

Create a Hashtable

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 mycountries and four elements//四个元素 are added:

<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

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 script that builds the list://创建列表

<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//子程序 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:

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

Note: You cannot choose the sort order//排序 of the items added to the Hashtable. To sort items alphabetically//按字母表顺序地 or numerically//数值, use the SortedList//分类列表 object.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: