ASP.NET XML 数据绑定
asp.net web forms - xml 文件
我们可以绑定 xml 文件到列表控件。
一个 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>
<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 控件
首先,导入 "system.data" 命名空间。我们需要该命名空间与 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
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>
<body>
<form runat="server">
<asp:radiobuttonlist id="rb" runat="server" autopostback="true" />
</form>
</body>
</html>
然后添加创建 xml dataset 的脚本,并且绑定 xml dataset 中的值到 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
</script>
<html>
<body>
<form runat="server">
<asp:radiobuttonlist id="rb" runat="server"
autopostback="true" onselectedindexchanged="displaymessage" />
</form>
</body>
</html>
<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 控件中的某个项目时,该子例程会被执行。当某个单选按钮被点击时,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>
<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>