当前位置: 首页 > 网络学院 > 服务端脚本教程 > ASP.NET > ASP.NET - The Hashtable Object 哈希表对象

ASP.NET
ASP.NET 2.0 - Navigation 导航
C#中new和override区别
ASP.NET中 Bin,App_Browser,App_code,App_Data,App_Theme 等文件

ASP.NET - The Hashtable Object 哈希表对象


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-01   浏览: 911 ::
收藏到网摘: n/a

The Hashtable object contains items in key/value pairs.
哈希表对象包含项目中的 键/值 对


Examples 举例

Example 1 - Hashtable RadioButtonList

Example 2 - Hashtable RadiobuttonList

Example 3 - Hashtable DropDownList


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.
使用Add()方法可以往哈希表中加入项目。

The following code creates a Hashtable named mycountries and four elements are added:
以下代码建立一名为 mycountries 的哈希表并且要加入四个元素:

<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:
要给RadioButtonList控件绑定数据,首先要在.aspx页面中建立一个RadioButtonList 控件(不需要任何asp:ListItem元素):

<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:
然后我们加入一个子程序,当用户点了RadioButtonList 控件中的一个项目后就会执行里面的代码。当按钮被点下,一段文字就会显示在标签里:

<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.
注意:你不能对哈希表中的项目进行排序选择。要以字母或是数字的方式进行排序要使用 SortedList 对象。

评论 (0) All

登陆 | 还没注册?