首页


ASP .NET教程

  • ASP .NET简介
  • ASP与ASP .NET
  • 安装ASP .NET
  • ASP .NET网页
  • ASP .NET控件
  • ASP .NET事件
  • ASP .NET表单

  • ASP .NET Form
  • ASP .NET ViewState
  • asp:TextBox
  • asp:Button
  • ASP.NET绑定

  • 数据绑定
  • ArrayList
  • Hashtable
  • SortedList
  • XML文件
  • asp:Repeater
  • asp:DataList
  • ASP.NET数据库

  • 数据库连接
  • 手册

  • HTML控件
  • Web控件
  • 验证控件
  • 示例

  • ASP .NET示例

  • ASP .NET - Hashtable对象

    [前一节] [后一节]

    Hashtable对象包含的是key/value组合形式的数据项。


    创建Hashtable

    Hashtable对象包含的是key/value组合形式的数据项。key被用作索引,通过检索key可以快速搜索到响应的value值。

    数据项用Add()方法添加到Hashtable。

    以下代码创建一个名为mycountries的Hashtable,并添加4个元素:

    <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元素):

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

    然后添加脚本来建立序列:

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

    然后我们加入一段子程序,它在用户点击RadioButtonList控件中的某一项时将被执行。当一个radio按钮被点击,文本将出现在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>

    注意:你无法选择对Hashtable数据项排序。要对数据项按字母或数字顺序排序,请使用SortedList对象。


    [前一节] [后一节]


    本站教程均为老猫根据外文资料翻译整理,将逐步刊出。此版本内容保证国内绝无仅有,由于时间、水平有限,有不妥之处欢迎指正。

    如果能对您有所帮助,敬请赞助,老猫不胜感激!

    1. 网上乞讨:
    在您任何方便的时候,向以下帐号存入10元人民币(当然越多越好)。
    交通银行太平洋卡
    601428 7091 5592604

    2. 广告投放:
    在任何指定页面投放各种形式广告,价格优惠。

    3. 代为宣传:
    以任何形式向您的亲朋好友推荐。

    Copyright © Tom.s Online 2003-2004