当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > ASP.NET入门教程:把XML文件绑定到列表控件

ASP.NET
在word中如何控制graph控件
把一个int数组的数字从小到大排列(C#)
Asp组件高级入门与精通系列之三
连接MYSQL数据库的方法及示例
SharePoint Portal Server之常见问题
软件开发中运用到的编号
VB程序员眼中的C#7
公农历转换VB类
VB程序员眼中的C#6
优化VB.NET应用程序的性能1
C#初学乍练-文本替换工具命令行版
如何得到某集合的所有子集合
VB程序员眼中的C#3
Shared Source CLI Essentials第一章第二部分
在ASP.NET使用javascript的一点小技巧
用户 'NT AUTHORITY\NETWORK SERVICE' 登录失败解决方法
开始使用SQLServer2005,没用过MSDE,一开始还不知道怎么下手呢,呵呵
我的ASP.net学习历程有关于.dll文件的迷惑
在图片上写字 C#
Shared Source CLI Essentials第一章第一部分

ASP.NET入门教程:把XML文件绑定到列表控件


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

前面我们讲述了SortedList对象,其实我们也可以把XML文件绑定到列表控件。 

我们可以把 XML 文件绑定到列表控件。

实例

例子 1 - XML RadiobuttonList

一个 XML 文件

这里有一个名为 "countries.xml" 的 XML 文件:

<?xml version="1.0" encoding="ISO-8859-1"?>
<countries>
<country>
<text>China</text>
<value>C</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>

把 DataSet 绑定到 List 控件

首先,导入 "System.Data" 命名空间。我们需要该命名空间与 DataSet 对象一起工作。把下面这条指令包含在 .aspx 页面的顶部:

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

接下来,为这个 XML 文件创建一个 DataSet,并在页面首先加载时把这个 XML 文件载入该 DataSet:看到本信息,说明该文章来源于软晨学习网www.ruanchen.com,如果文章不完整请到软晨学习网ruanchen.com浏览!

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

如需把该 DataSet 绑定到 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>
<%@ 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>