Web Forms 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>
#綁定DataSet 到List 控制項首先,匯入"System.Data" 命名空間。我們需要該命名空間與 DataSet 物件一起工作。把下面這條指令包含在.aspx 頁面的頂端:
<%@ Import Namespace="System.Data" %>##接著,為XML檔案建立一個DataSet,在頁面第一次載入時把這個XML 檔案載入DataSet:
<script runat="server">sub Page_Load
為了綁定資料到RadioButtonList 控制項,首先要在.aspx 頁面中建立一個RadioButtonList控制項(不帶任何asp:ListItem 元素):if Not Page.IsPostBack then
dim mycountries=New DataSet
## mycountries.ReadXml(MapPath("countries.xml"))
end if
end sub
dim mycountries=New DataSet
## mycountries.ReadXml(MapPath("countries.xml"))
end if
end sub
<html>
#########################################################################################################################################################################<body>
<form runat="server">
<asp:RadioButtonList id="rb" runat="server" AutoPostBack="True" />
</form>
#</body>
</html>
<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><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>
#然後我們新增一個子例程,當使用者點擊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> <!DOCTYPE html> <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>##執行實例»點擊"運行實例" 按鈕查看線上實例