ASP.NET - XML 파일
- 이전 페이지 WebForms SortedList
- 다음 페이지 WebForms Repeater
XML 파일을 목록 컨트롤에 바인딩할 수 있습니다.
XML 파일
이름이 "countries.xml"인 XML 파일이 있습니다:
<?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>
이 파일을 확인하세요:countries.xml
DataSet을 List 컨트롤에 바인딩하십시오
먼저 "System.Data" 네임스페이스를 가져오세요. DataSet 객체와 함께 작업할 필요가 있습니다. 아래 명령어를 .aspx 페이지의 상단에 포함하세요:
<%@ Import Namespace="System.Data" %>
그런 다음, 이 XML 파일에 DataSet을 생성하고, 페이지가 첫 번째로 로드될 때 XML 파일을 DataSet에 로드하세요:
<script runat="server"> sub Page_Load if Not Page.IsPostBack then dim mycountries=New DataSet mycountries.ReadXml(MapPath("countries.xml")) end if end sub
DataSet을 RadioButtonList 컨트롤에 바인딩하려면, 먼저 .aspx 페이지에서 RadioButtonList 컨트롤을 생성하세요 (asp:ListItem 요소가 없습니다):
<html> <body> <form runat="server"> <asp:RadioButtonList id="rb" runat="server" AutoPostBack="True" /> </form> </body> </html>
그런 다음, 이 XML DataSet을 구성하는 스크립트를 추가합니다:
<%@ 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>
- 이전 페이지 WebForms SortedList
- 다음 페이지 WebForms Repeater