(2)用XML文檔填充列表框,這裡只用到<name>節點.
<%@ Page Language="C#" Debug="true" %>
< %@ Import Namespace="System.Collections" %>
< %@ Import Namespace="System.XML" %>
< Html>
< head>
< script language="C#" runat="server">
private void Page_Load(Object sender, EventArgs e)
{
if (!IsPostBack)
{
ArrayList values = new ArrayList();//將其當作數據源
XmlDocument doc = new XmlDocument();//表示XML文檔
doc.Load(Server.MapPath("people.XML"));
//返回一個XMLNodeList集合,包含與指定名稱匹配的所有子代元素列表
XmlNodeList elemList = doc.GetElementsByTagName("name");//System.XML 命名空間
for (int i=0; i < elemList.Count; i++)
{
values.Add (elemList[i].InnerXML);
}
ListBox1.DataSource = values;
ListBox1.DataBind();
}
}
//得到列表項,被選中項的文本
private void SubmitBtn_Click(Object sender, EventArgs e)
{
if(ListBox1.SelectedIndex>-1)
Label1.Text = "Selected Option: " + ListBox1.SelectedItem.Text + "<p>";
}
< /script>
< /head>
< body>
< form runat=server>
< h3>Data Binding ListBox</h3>
< ASP:Label id="Label1" font-name="Verdana" font-size="10pt" runat="server"/>
< ASP:ListBox id="ListBox1" SelectionMode="Single" Rows="1" runat="server"/>
< ASP:button id="Button1" Text="Submit" OnClick="SubmitBtn_Click" runat="server"/>
< /form>
< /body>
< /Html>
==============================================