dati XML ASP.NET vincolanti
Siamo in grado di associare un file XML per il controllo elenco.
Un file XML
C'è un file chiamato file XML "countries.xml":
<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>
Controllare il file XML: countries.xml
DataSet Bind to List Controls
In primo luogo, importare il namespace "System.Data". Abbiamo bisogno di questo spazio dei nomi per lavorare con oggetti DataSet. La seguente istruzione è incluso nella parte superiore della pagina aspx:
Successivamente, creare un set di dati per il file XML, e quando la pagina viene caricata questa XML caricamento del file DataSet:
sub Page_Load
if Not Page.IsPostBack then
dim mycountries=New DataSet
mycountries.ReadXml(MapPath("countries.xml"))
end if
end sub
Per associare i dati a un controllo RadioButtonList, prima creare un controllo RadioButtonList a una pagina aspx (senza asp: ListItem elementi):
<body>
<form runat="server">
<asp:RadioButtonList id="rb" runat="server" AutoPostBack="True" />
</form>
</body>
</html>
Quindi aggiungere lo script per creare DataSet XML, e si lega il valore al controllo XML DataSet RadioButtonList:
<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>
Poi aggiungiamo una subroutine, quando un utente fa clic su un elemento nel controllo RadioButtonList quando viene eseguito il sottoprogramma. Quando un pulsante viene premuto, l'etichetta apparirà nella riga di testo:
Esempi
<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 dimostrazione >>