ASP頁面:Code
<asp:DropDownList ID="compactType" runat="server" AutoCallBack="True" Width="153px"> </asp:DropDownList>
Xml檔案文檔Code
<?xml version="1.0" encoding="utf-8" ?>
<roots>
<root>
<id>1</id>
<Culture>初中以下</Culture>
</root>
<root>
<id>2</id>
<Culture>初中</Culture>
</root>
<root>
<id>3</id>
<Culture>中專</Culture>
</root>
<root>
<id>4</id>
<Culture>高中</Culture>
</root>
<root>
<id>5</id>
<Culture>大專</Culture>
</root>
<root>
<id>6</id>
<Culture>本科</Culture>
</root>
</roots>
<3>讀取xmlwen見進行綁定的方法---M
Code
/// <summary>
/// 讀取xml檔案,用資料填充DropDownList,進行綁定
/// </summary>
/// <param name="path">xml檔案路徑</param>
/// <param name="dp">要進行綁定的DropDownList名稱</param>
/// <param name="id">DropDownList要顯示的文本(xml檔案的一個節點)</param>
/// <param name="val">DropDownList要顯示的值(xml檔案的一個節點)</param>
public void ReadXml(string path,DropDownList dp,string id,string val)
{
DataSet ds = new DataSet();
ds.ReadXml(path);
dp.DataSource = ds;
dp.DataTextField = id ;
dp.DataValueField = val;
dp.DataBind();
}
<4>調用Code
SecurityFactory sf = new SecurityFactory();
//xml檔案路徑
string path2 = Server.MapPath("./xml/XMLFile2.xml");
sf.ReadXml(path2, this.compactType, "id", "val");
綁定成功!