ASP.NET - XML dosyası

XML dosyasını liste denetimi ile bağlayabiliriz.

Bir XML dosyası

Burada "countries.xml" adında bir XML dosyası var:

<?xml version="1.0" encoding="ISO-8859-1"?>
<countries>
<country>
  <text>China</text>
  <value>C</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>

Bu dosyayı kontrol edin:countries.xml

DataSet'i List denetimi ile bağla

Öncelikle "System.Data" adlı adlandırma alanını içe aktarın. Bu adlandırma alanını DataSet nesnesi ile birlikte çalışmak için ihtiyacımız var. Aşağıdaki komutu .aspx sayfasının üstüne ekleyin:

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

Sonra, bu XML dosyası için bir DataSet oluşturun ve sayfa ilk yüklendiğinde bu XML dosyasını DataSet'e yükleyin:

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

Bu DataSet'yi RadioButtonList denetimi ile bağlamak istiyorsanız, öncelikle .aspx sayfasında bir RadioButtonList denetimi oluşturun (hiçbir asp:ListItem elementi olmamalı):

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

Sonra bu XML DataSet'ini oluşturacak scripti ekleyin:}

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

Sonra, kullanıcı RadioButtonList kontrolündeki bir öğeye tıkladığında çalışacak bir alt prosedür ekliyoruz. Kullanıcı bir radyo düğmesine tıkladığında, etikette bir metin görünür olacaktır:

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

Bu Örneği Göster