📄 aspnet_xml.asp@output=print
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en-US" xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>ASP.NET Data Binding with XML</title>
<link rel="shortcut icon" href="../favicon.ico" type="image/x-icon" />
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<meta name="Keywords" content="xml,tutorial,html,dhtml,css,xsl,xhtml,javascript,asp,ado,vbscript,dom,sql,colors,soap,php,authoring,programming,training,learning,beginner's guide,primer,lessons,school,howto,reference,examples,samples,source code,tags,demos,tips,links,FAQ,tag list,forms,frames,color table,w3c,cascading style sheets,active server pages,dynamic html,internet,database,development,Web building,Webmaster,html guide" />
<meta name="Description" content="Free HTML XHTML CSS JavaScript DHTML XML DOM XSL XSLT RSS AJAX ASP ADO PHP SQL tutorials, references, examples for web building." />
<meta http-equiv="pragma" content="no-cache" />
<meta http-equiv="cache-control" content="no-cache" />
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "../../https@ssl./default.htm" : "../../www./default.htm");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-3855518-1");
pageTracker._initData();
pageTracker._trackPageview();
</script>
</head>
<body>
<p>From <b>http://www.w3schools.com</b> (Copyright Refsnes Data)</p>
<h1>ASP .NET - XML Files</h1>
<a href="aspnet_sortedlist.asp"><img alt="previous" border="0" src="../images/btn_previous.gif" /></a>
<a href="aspnet_repeater.asp"><img alt="next" border="0" src="../images/btn_next.gif" /></a>
<hr />
<p class="intro">We can bind an XML file to a list control.</p>
<hr />
<h2>Examples</h2>
<p><a target="_blank" href="showasp.asp@filename=demo_xml_radio1">Example 1 -
XML RadiobuttonList</a></p>
<hr />
<h2>An XML File</h2>
<p>Here is an XML file named "countries.xml":</p>
<table class="ex" border="1" width="100%">
<tr><td>
<pre><?xml version="1.0" encoding="ISO-8859-1"?></pre>
<pre><countries></pre>
<pre><country>
<text>Norway</text>
<value>N</value>
</country></pre>
<pre><country>
<text>Sweden</text>
<value>S</value>
</country></pre>
<pre><country>
<text>France</text>
<value>F</value>
</country></pre>
<pre><country>
<text>Italy</text>
<value>I</value>
</country></pre>
<pre></countries></pre>
</td></tr>
</table>
<p>Take a look at the XML file: <a target="_blank" href="countries.xml">countries.xml</a></p>
<hr />
<h2>Bind a DataSet to a List Control</h2>
<p>First, import the "System.Data"
namespace. We need this namespace to work with DataSet objects. Include the
following directive at the top of an .aspx page:</p>
<table class="ex" border="1" width="100%">
<tr><td>
<pre><%@ Import Namespace="System.Data" %></pre>
</td></tr>
</table>
<p>Next, create a DataSet for the XML file and load the XML file into the DataSet when the page is first loaded:</p>
<table class="ex" border="1" width="100%">
<tr><td>
<pre><script runat="server">
sub Page_Load
if Not Page.IsPostBack then
dim mycountries=New DataSet
mycountries.ReadXml(MapPath("countries.xml"))
end if
end sub</pre>
</td></tr>
</table>
<p>To bind the DataSet to a RadioButtonList control, first create a RadioButtonList control (without
any asp:ListItem elements) in an .aspx page:</p>
<table class="ex" border="1" width="100%">
<tr><td>
<pre><html>
<body></pre>
<pre><form runat="server">
<asp:RadioButtonList id="rb" runat="server"
AutoPostBack="True" /><br /></form></pre>
<pre></body>
</html></pre>
</td></tr>
</table>
<p>Then add the script that builds the XML DataSet:</p>
<table class="ex" border="1" width="100%">
<tr><td>
<pre><%@ Import Namespace="System.Data" %></pre>
<pre><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></pre>
<pre><html>
<body></pre>
<pre><form runat="server">
<asp:RadioButtonList id="rb" runat="server"
AutoPostBack="True" onSelectedIndexChanged="displayMessage" />
</form></pre>
<pre></body>
</html></pre>
</td></tr>
</table>
<p>Then we add a sub routine to be executed when the user clicks on an item in the RadioButtonList control. When
a radio button is clicked, a text will appear in a label:</p>
<table class="ex" border="1" width="100%">
<tr><td>
<pre><%@ Import Namespace="System.Data" %></pre>
<pre><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</pre>
<pre>sub displayMessage(s as Object,e As EventArgs)
lbl1.text="Your favorite country is: " & rb.SelectedItem.Text
end sub
</script></pre>
<pre><html>
<body></pre>
<pre><form runat="server">
<asp:RadioButtonList id="rb" runat="server"
AutoPostBack="True" onSelectedIndexChanged="displayMessage" />
<p><asp:label id="lbl1" runat="server" /></p>
</form></pre>
<pre></body>
</html></pre>
</td></tr>
</table>
<br />
<hr />
<a href="aspnet_sortedlist.asp"><img alt="previous" border="0" src="../images/btn_previous.gif" /></a>
<a href="aspnet_repeater.asp"><img alt="next" border="0" src="../images/btn_next.gif" width="100" height="20" /></a>
<p>From <b>http://www.w3schools.com</b> (Copyright Refsnes Data)</p>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -