当前位置: 首页 > 网络学院 > 服务端脚本教程 > ASP.NET > ASP.NET - XML 文件

ASP.NET
ASP.NET 介绍
ASP 和 ASP.NET 之间的区别
asp.net的安装
asp.net的网页
ASP.NET - 服务器控件
ASP.NET - 事件
ASP.NET Web 表单
ASP.NET 维持浏览状态
ASP.NET - TextBox(文本框)控件
ASP.NET - 按钮控件
ASP.NET - Data Binding(数据绑定)
ASP.NET - The ArrayList Object(数组列表对象)
ASP.NET - The Hashtable Object 哈希表对象
ASP.NET - The SortedList Object 可排序列表对象
ASP.NET - XML 文件
ASP.NET - Repeater Control 转发器控件
ASP.NET - The DataList Control 数据列表控件
ASP.NET - Database Connection 数据库连接
ASP.NET 2.0 新特征
ASP.NET 2.0 - Master Pages 控制页[主页]

ASP.NET - XML 文件


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

We can bind an XML file to a list control.
我们可以将XML文件绑定到列表控件中。


Examples 举例

Example 1 - XML RadiobuttonList


An XML File
XML 文件

Here is an XML file named "countries.xml":
这是一个XML文件,名称为“countries.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>

Take a look at the XML file: countries.xml
查看一下这个文件:countries.xml


Bind a DataSet to a List Control
将数据集绑定到列表控件

First, import the "System.Data" namespace. We need this namespace to work with DataSet objects. Include the following directive at the top of an .aspx page:
首先,导入命名空间“System.Data”。我们需要这个命名空间来与数据集对象一起运作。在.aspx页面上顶部加入以下的指示:

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

Next, create a DataSet for the XML file and load the XML file into the DataSet when the page is first loaded:
接下来,为XML文件建立一个数据集并且当页面第一次加载的时候往数据集中载入XML文件:

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

To bind the DataSet to a RadioButtonList control, first create a RadioButtonList control (without any asp:ListItem elements) in an .aspx page:
将数据集绑定到RadioButtonList控件中,首先在.aspx页面中建立RadioButtonList控件:

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

Then add the script that builds the XML DataSet:
然后加入脚本来生成XML数据集:

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

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 控件里的项目后就会执行。当单选按钮被按下,就会在标签里出现文字:

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

评论 (0) All

登陆 | 还没注册?