ASP.NET XML 數(shù)據(jù)綁定
asp.net web forms - xml 文件
我們可以綁定 xml 文件到列表控件。
一個(gè) xml 文件
這里有一個(gè)名為 "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>
查看這個(gè) xml 文件:countries.xml
綁定 dataset 到 list 控件
首先,導(dǎo)入 "system.data" 命名空間。我們需要該命名空間與 dataset 對象一起工作。把下面這條指令包含在 .aspx 頁面的頂部:
<%@ import namespace="system.data" %>
接著,為 xml 文件創(chuàng)建一個(gè) dataset,并在頁面第一次加載時(shí)把這個(gè) 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
為了綁定數(shù)據(jù)到 radiobuttonlist 控件,首先要在 .aspx 頁面中創(chuàng)建一個(gè) 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>
然后添加創(chuàng)建 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>
然后我們添加一個(gè)子例程,當(dāng)用戶點(diǎn)擊 radiobuttonlist 控件中的某個(gè)項(xiàng)目時(shí),該子例程會(huì)被執(zhí)行。當(dāng)某個(gè)單選按鈕被點(diǎn)擊時(shí),label 中會(huì)出現(xiàn)一行文本:
實(shí)例
<%@ 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>