建立XML檔與控制項資料連繫

摘要:建立XML檔與控制項資料連繫

<asp:DropDownList ID="dropContry" runat="server" DataSourceID="ContryList" DataTextField="Name"  DataValueField="ContryCode" >
</asp:DropDownList>

------------------判斷下拉式選單是否已選擇----------------------------------------------------------------------
<asp:RequiredFieldValidator id="reqFavoriteColor" Text="請選擇國家" InitialValue=""
ControlToValidate="dropContry" Runat="server" Display="Dynamic"/>

<asp:XmlDataSource ID="ContryList" runat="server" DataFile="~/App_Data/ContrySelect.xml" XPath="Contry/ContrySelect">
</asp:XmlDataSource>  


------------------------------------- XML檔 ----------------------------------------------------
<?xml version="1.0" encoding="utf-8" ?>
<Contry>
  <ContrySelect Name="--請選擇國家--" ContryCode=""/>
  <ContrySelect Name="Afghanistan" ContryCode="Afghanistan"/>
  <ContrySelect Name="Albania" ContryCode="Albania"/>
  <ContrySelect Name="Algeria" ContryCode="Algeria"/>
  <ContrySelect Name="American Samoa" ContryCode="American Samoa"/>
  <ContrySelect Name="Andorra" ContryCode="Andorra"/>
  <ContrySelect Name="Angola" ContryCode="Angola"/>
  <ContrySelect Name="Anguilla" ContryCode="Anguilla"/>
</Contry>