ÍøÂçѧԺ w3popÉçÇø ÍøÂç×ÊÔ´ ITÐÂÎÅ

w3pop.com :: ÍøÂçѧԺ :: ASP.NET :: ASP.NET - XML Îļþ

»áÔ±µÇ½

ÕʺÅ

ÃÜÂë

»Ø´ð

¼ÇסÃÜÂë

Íü¼ÇÃÜÂë? ×¢²á

ASP.NET
asp.netµÄ°²×°
ASP ºÍ ASP.NET Ö..
ASP.NET ½éÉÜ

ASP.NET - XML Îļþ


×÷Õß:w3schools ·­Òë/ÕûÀí:w3pop.com ·¢²¼:2007-08-08 ä¯ÀÀ:2625 :: ::

We can bind an XML file to a list control.
ÎÒÃÇ¿ÉÒÔ½«XMLÎļþ°ó¶¨µ½ÁÐ±í¿Ø¼þÖС£


Examples ¾ÙÀý

Example 1 - XML RadiobuttonList


An XML File
XML Îļþ

Here is an XML file named "countries.xml":
ÕâÊÇÒ»¸öXMLÎļþ£¬Ãû³ÆÎª“countries.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>

Take a look at the XML file: countries.xml
²é¿´Ò»ÏÂÕâ¸öÎļþ£ºcountries.xml


Bind a DataSet to a List Control
½«Êý¾Ý¼¯°ó¶¨µ½ÁÐ±í¿Ø¼þ

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:
Ê×ÏÈ£¬µ¼ÈëÃüÃû¿Õ¼ä“System.Data”¡£ÎÒÃÇÐèÒªÕâ¸öÃüÃû¿Õ¼äÀ´ÓëÊý¾Ý¼¯¶ÔÏóÒ»ÆðÔË×÷¡£ÔÚ.aspxÒ³ÃæÉ϶¥²¿¼ÓÈëÒÔϵÄָʾ£º

<%@ Import Namespace="System.Data" %>

Next, create a DataSet for the XML file and load the XML file into the DataSet when the page is first loaded:
½ÓÏÂÀ´£¬ÎªXMLÎļþ½¨Á¢Ò»¸öÊý¾Ý¼¯²¢ÇÒµ±Ò³ÃæµÚÒ»´Î¼ÓÔØµÄʱºòÍùÊý¾Ý¼¯ÖÐÔØÈëXMLÎļþ£º

<script runat="server">
sub Page_Load
if Not Page.IsPostBack then
dim mycountries=New DataSet
mycountries.ReadXml(MapPath("countries.xml"))
end if
end sub

To bind the DataSet to a RadioButtonList control, first create a RadioButtonList control (without any asp:ListItem elements) in an .aspx page:
½«Êý¾Ý¼¯°ó¶¨µ½RadioButtonList¿Ø¼þÖУ¬Ê×ÏÈÔÚ.aspxÒ³ÃæÖн¨Á¢RadioButtonList¿Ø¼þ£º

<html>
<body>
<form runat="server">
<asp:RadioButtonList id="rb" runat="server"
AutoPostBack="True" />
</form>
</body>
</html>

Then add the script that builds the XML DataSet:
È»ºó¼ÓÈë½Å±¾À´Éú³ÉXMLÊý¾Ý¼¯£º

<%@ 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>

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:
ÎÒÃÇÔÙÍùÀï¼ÓÈë×Ó³ÌÐò£¬µ±Óû§µã»÷ÁË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
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>

ÆÀÂÛ (0) All