ล่าสุดการพัฒนาเว็บบทเรียน
×

ASP.NET เกี่ยวกับการสอน

ASP.NET บ้าน ASP.NET แนะนำ

WP เกี่ยวกับการสอน

WebPages แนะนำ WebPages มีดโกน WebPages แบบ WebPages โฟลเดอร์ WebPages สถานการณ์โดยรวม WebPages ฟอร์ม WebPages วัตถุ WebPages ไฟล์ WebPages ฐานข้อมูล WebPages ผู้ช่วย WebPages WebGrid WebPages ชาร์ต WebPages อีเมล์ WebPages PHP WebPages ประกาศ WebPages ตัวอย่าง

WP คู่มืออ้างอิง

WebPages ชั้นเรียน WebPages ความปลอดภัย WebPages ฐานข้อมูล WebPages เว็บเมล์ WebPages ผู้ช่วย

ASP.NET Razor

Razor แนะนำ Razor วากยสัมพันธ์ Razor C# ตัวแปร Razor C# ลูป Razor C# ตรรกะ Razor VB ตัวแปร Razor VB ลูป Razor VB ตรรกะ

ASP.NET MVC

MVC แนะนำ MVC ใบสมัคร MVC โฟลเดอร์ MVC แบบ MVC ตัวควบคุม MVC เข้าชม MVC ฐานข้อมูล MVC แบบ MVC ความปลอดภัย MVC HTML ผู้ช่วย MVC ประกาศ MVC การอ้างอิง

WF สอน

WebForms แนะนำ WebForms หน้า WebForms การควบคุม WebForms เหตุการณ์ WebForms ฟอร์ม WebForms ViewState WebForms กล่องข้อความ WebForms ปุ่ม WebForms ข้อมูลผูกพัน WebForms ArrayList WebForms Hashtable WebForms SortedList WebForms XML ไฟล์ WebForms Repeater WebForms DataList WebForms DbConnection WebForms Master หน้า WebForms การเดินเรือ WebForms ตัวอย่าง


 

ASP.NETเว็บฟอร์ม - ไฟล์ XML


เราสามารถผูกไฟล์ XML เพื่อควบคุมรายการ


ไฟล์ 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>

ลองดูที่ไฟล์ XML นี้: countries.xml


ผูกชุดข้อมูลเป็นตัวควบคุมรายการ

ครั้งแรกที่นำเข้า "System.Data" namespace เราจำเป็นต้อง namespace นี้จะทำงานร่วมกับชุดข้อมูลวัตถุ รวมถึงคำสั่งดังต่อไปนี้ที่ด้านบนของหน้า .aspx นี้:

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

ถัดไปสร้างชุดข้อมูลสำหรับไฟล์ 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

การผูกชุดข้อมูลที่จะควบคุม RadioButtonList แรกสร้างตัวควบคุม RadioButtonList (ไม่มีงูเห่าใดองค์ประกอบ ListItem) ในหน้าขอบ:

<html>
<body>

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

</body>
</html>

จากนั้นเพิ่มสคริปต์ที่สร้างชุดข้อมูล 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>

จากนั้นเราก็เพิ่มประจำย่อยจะต้องถูกประหารชีวิตเมื่อผู้ใช้คลิกที่รายการในการควบคุม 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>
แสดงตัวอย่าง»