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

ASP.NET
ASP.NET(C#)
ASP.NET入门数据篇
C#Web应用程序入门经典学习笔记之一
ASP.NET2.0 WebRource,开发微调按钮控件
介绍几个ASP.NET中容易忽略但却很重要的方法函数
ASP.Net2.0 GridView 多列排序,显示排序图标,分页
ASP.NET 2.0 中的创建母版页
asp,asp.net学习教程下载
ASP.Net生成一个简单的图片
FCKeditor.Net_2.2安全修正版
ASP.NET与数据库相关技巧
在asp.net下实现Option条目中填充前导空格的方法
在ASP.NET中用MSDNURLRewriting实现Url Rewriting
在ASP.NET中实现多文件上传的方法
Community Server专题二:体系结构
在ASP.NET中重写URL的代码
asp.net下大文件上传知识整理
ASP.NET中常用的三十三种代码
asp.net下实现支持文件分块多点异步上传的 Web Services
ASP.NET 2.0,C#----图像特效处理

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-28   浏览: 75 ::
收藏到网摘: 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>