首页


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 - SortedList对象

    [前一节] [后一节]

    SortedList对象结合了ArrayList对象和Hashtable对象的功能。


    SortedList对象

    SortedList对象包含key/value组合形式的数据项。SortedList对象自动对数据项以字母或数字顺序排序。

    数据项用Add()方法添加到SortedList。可以使用TrimToSize()方法限定SortedList为其最终的实际大小。

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

    <script runat="server">
    sub Page_Load
    if Not Page.IsPostBack then
      dim mycountries=New SortedList
      mycountries.Add("N","Norway")
      mycountries.Add("S","Sweden")
      mycountries.Add("F","France")
      mycountries.Add("I","Italy")
    end if
    end sub
    </script>


    数据绑定

    SortedList对象可以对下列控件自动生成文本和值:

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


    [前一节] [后一节]


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

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

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

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

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

    Copyright © Tom.s Online 2003-2004