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

ASP.NET
如何在ASP.NET中使用SmtpMail发送邮件
在VB.NET中利用Split和Replace函数计算字数
Attribute应用:简化ANF自定义控件初始化过程
ASP.NET 2.0移动开发入门之使用样式
ASP.NET 2.0中使用OWC生成图表
ASP.NET 2.0中控件的简单异步回调
一个无法捕获ADO.NET Dataset的内存错误
深入解读ADO.NET2.0的十大最新特性
.Net平台下的分布式缓存设计
ASP.NET全局异常处理浅析
ASP.NET 2.0中文验证码的实现
浅析.NET平台编程语言的未来走向
.net 框架程序设计收藏
使用ASP.NET MVC Futures 中的异步Action
详解.NET中的XmlReader与XmlWriter
关于.NET中的Server push技术
asp.net页面执行机制
对比JSP和ASP.NET的存储过程
.NET 4.0不会包含System.Shell.CommandLine
ASP.NET十个有效性能优化的方法

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


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