首页>教程>ASP.NET教程>ASP.NET Web Forms Hashtable 对象

需要支持?

如果通过文档没办法解决您的问题,请提交工单获取我们的支持!

ASP.NET Web Forms Hashtable 对象

ASP.NET Web Forms Hashtable 对象包含用键/值对表示的项目

范例

  • Hashtable RadiobuttonList 1
<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>
<meta charset="utf-8" />
<form runat="server">
<asp:RadioButtonList id="rb" runat="server"
AutoPostBack="True" onSelectedIndexChanged="displayMessage" />
<p><asp:label id="lbl1" runat="server" /></p>
</form>
  • Hashtable RadiobuttonList 2
<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>

<!DOCTYPE html>
<meta charset="utf-8" />
<form runat="server">
<asp:RadioButtonList id="rb" runat="server"
AutoPostBack="True" onSelectedIndexChanged="navigate" />
</form>
  • Hashtable DropDownList
<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>

<!DOCTYPE html>
<meta charset="utf-8" />
<form runat="server">
<asp:DropDownList id="dd" runat="server"
AutoPostBack="True" onSelectedIndexChanged="displayMessage" />
<p><asp:label id="lbl1" runat="server" /></p>
</form>

创建 Hashtable

Hashtable 对象包含用键/值对表示的项目

键被用作索引,通过搜索键,可以实现对值的快速搜索

通过 Add() 方法向 Hashtable 添加项目

下面的代码创建了一个名为 mycountries 的 Hashtable 对象,并添加了四个元素

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

数据绑定

Hashtable 对象可为下列的控件自动生成文本和值

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

为了绑定数据到 RadioButtonList 控件,首先要在 .aspx 页面中创建一个 RadioButtonList 控件(不带任何 asp:ListItem 元素)

<!DOCTYPE html>
<meta charset="utf-8" />
<form runat="server">
    <asp:RadioButtonList id="rb" runat="server" AutoPostBack="True" />
</form>

然后添加创建列表的脚本,并且绑定列表中的值到 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
</script>
<!DOCTYPE html>
<meta charset="utf-8" />
<form runat="server">
    <asp:RadioButtonList id="rb" runat="server" AutoPostBack="True" />
</form>

然后我们添加一个子例程,当用户点击 RadioButtonList 控件中的某个项目时,该子例程会被执行

当某个单选按钮被点击时,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>
<!DOCTYPE html>
<meta charset="utf-8" />
<form runat="server">
<asp:RadioButtonList id="rb" runat="server" AutoPostBack="True"  onSelectedIndexChanged="displayMessage" />
<p><asp:label id="lbl1" runat="server" /></p>
</form>

注意 : 我们无法选择添加到 Hashtable 的项目的排序方式,如果需要对项目进行字母排序或者数字排序,请使用 SortedList 对象

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
今日签到
有新私信 私信列表
搜索