首页


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 - XML文件

    [前一节] [后一节]

    我们可以绑定一个XML文件到一个list控件。


    XML文件

    这是一个名为“countries.xml”的XML文件:

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <countries>
    <country>
    <text>Norway</text>
    <value>N</value>
    </country>
    <country>
    <text>Sweden</text>
    <value>S</value>
    </country>
    <country>
    <text>France</text>
    <value>F</value>
    </country>
    <country>
    <text>Italy</text>
    <value>I</value>
    </country>
    </countries>

    我们看一下这个XML文件:countries.xml


    绑定DataSet到List控件

    首先,导入namespace “System.Data”。我们需要这个namespace与DataSet对象一起工作。在一个.aspx页面的顶部包含这个指令:

    <%@ Import Namespace="System.Data" %>

    接着,为XML文件创建一个DataSet并在页面首次被调入的时候加载XML文件到DataSet中:

    <script runat="server">
    sub Page_Load
    if Not Page.IsPostBack then
      dim mycountries=New DataSet
      mycountries.ReadXml(MapPath("countries.xml"))
    end if
    end sub

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

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

    然后添加建立XML DataSet的脚本:

    <%@ Import Namespace="System.Data" %>
    <script runat="server">
    sub Page_Load
    if Not Page.IsPostBack then
      dim mycountries=New DataSet
      mycountries.ReadXml(MapPath("countries.xml"))
      rb.DataSource=mycountries
      rb.DataValueField="value"
      rb.DataTextField="text"
      rb.DataBind()
    end if
    end sub
    </script>
    <html>
    <body>
    <form runat="server">
    <asp:RadioButtonList id="rb" runat="server"
    AutoPostBack="True" onSelectedIndexChanged="displayMessage" />
    </form>
    </body>
    </html>

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

    <%@ Import Namespace="System.Data" %>
    <script runat="server">
    sub Page_Load
    if Not Page.IsPostBack then
      dim mycountries=New DataSet
      mycountries.ReadXml(MapPath("countries.xml"))
      rb.DataSource=mycountries
      rb.DataValueField="value"
      rb.DataTextField="text"
      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