Latest web development tutorials

el enlace de datos XML de ASP.NET

Podemos enlazar un archivo XML para el control de lista.


Un archivo XML

Hay un archivo llamado "countries.xml" archivo 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>

Compruebe el archivo XML: countries.xml


Conjunto de datos se unen a la lista de controles

En primer lugar, importar el espacio de nombres "System.Data". Necesitamos este espacio de nombres para trabajar con objetos DataSet. Las siguientes instrucciones se incluye en la parte superior de la página .aspx:

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

A continuación, cree un conjunto de datos para el archivo XML, y cuando la página se carga por primera este XML archivo de conjunto de datos de carga:

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

Para enlazar datos a un control RadioButtonList, en primer lugar crear un control RadioButtonList en una página .aspx (sin asp: elementos ListItem):

<html>
<body>

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

</body>
</html>

A continuación, añadir el script para crear conjunto de datos XML, y se une el valor con el control conjunto de datos XML 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>

A continuación, añadimos una subrutina, cuando un usuario hace clic en un elemento en el control RadioButtonList cuando se ejecuta la subrutina. Cuando se hace clic en un botón de opción, la etiqueta aparecerá en la línea de texto:

Ejemplos

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

La demostración >>