asp.net聯動列表框

介紹

asp.net聯動列表框 另類刷新聯動下拉列表框(二級+XMLHttpRequest)
二級的聯動下拉列表,從思路上講,大致分下列幾步:
一:觸發第一個下拉列表的onchange事件。
二:異步調用處理頁後,會返回處理請求後的信息(伺服器端用Response.Write方法寫入要返回的字元串,客戶端用responseText屬性接收字元串。
三:用得到的字元串處理後更新第二個下拉列表框

主要代碼如下:

一:建立讀取XML的類,這裡也可以是資料庫的查詢
public class ReadXmlCS
{
public const string k = "|";
public const string s = ",";
public ReadXmlCS()
{
//
// TODO: 在此處添加構造函式邏輯
//
}
public static string QueryXml(string filename,string querystr)// @"//Pro_class/@id"
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
System.Xml.XPath.XPathDocument myXPathDocument = new System.Xml.XPath.XPathDocument(filename);
System.Xml.XPath.XPathNavigator myXPathNavigator = myXPathDocument.CreateNavigator();
try
{
System.Xml.XPath.XPathNodeIterator myXPathNodeIterator = myXPathNavigator.Select(querystr);
while(myXPathNodeIterator.MoveNext())
{
sb.Append(myXPathNodeIterator.Current.Value.ToString() +",");//把屬性值添加到字元串末尾,添加分組字元“,”
System.Xml.XPath.XPathNavigator myXPathNavigator2 = myXPathNodeIterator.Current.Clone();//克隆當前接點
// while(myXPathNavigator2.MoveToNextAttribute())//移動指針到下一個屬性
// {
// sb.Append(myXPathNavigator2.Value.ToString()+"|");//把屬性值添加到字元串末尾,添加分組字元“|”
// }
myXPathNavigator2.MoveToNextAttribute();
sb.Append(myXPathNavigator2.Value.ToString()+"|");
}
return sb.ToString();
}
catch{return null;}
}
public static void QueryXml(string filename,string querystr,System.Web.UI.WebControls.DropDownList dd)// @"//Pro_class/@id"
{
System.Xml.XPath.XPathDocument myXPathDocument = new System.Xml.XPath.XPathDocument(filename);
System.Xml.XPath.XPathNavigator myXPathNavigator = myXPathDocument.CreateNavigator();
try
{
System.Xml.XPath.XPathNodeIterator myXPathNodeIterator = myXPathNavigator.Select(querystr);
while(myXPathNodeIterator.MoveNext())
{
System.Web.UI.WebControls.ListItem li = new System.Web.UI.WebControls.ListItem();
li.Value = myXPathNodeIterator.Current.Value.ToString() ;//把屬性值(id)添加到ListItem的“值”屬性
System.Xml.XPath.XPathNavigator myXPathNavigator2 = myXPathNodeIterator.Current.Clone();//克隆當前接點
// while(myXPathNavigator2.MoveToNextAttribute())//移動指針到下一個屬性
// {
// li.Text = myXPathNavigator2.Value.ToString();//把屬性值(title)添加到ListItem的“文本”屬性
// }
myXPathNavigator2.MoveToNextAttribute();
li.Text = myXPathNavigator2.Value.ToString();
dd.Items.Add(li);
}
}
catch{}
}
}
其中關鍵的步驟加了注釋,其它的大家可以到MSDN上去查找。
二。實現 (WebForm8.aspx.cs頁面):
protected System.Web.UI.WebControls.DropDownList DropDownList2;
protected System.Web.UI.WebControls.DropDownList Dropdownlist1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此處放置用戶代碼以初始化頁面
string str = this.Request.QueryString【"querychild"】;
if((str != null) && (str == "yes"))
{
string querystr = "//Pro_class【" + this.Request【"parentid"】 + "】/Pro_class_small/@id";
string show = zlp.str.ReadXmlCS.QueryXml(@"D:\Inetpub\wwwroot\zlp\XmlData\ProductMenu.xml",querystr);
this.Response.Write(show);
this.Response.End();
}
if(!this.IsPostBack)
{
this.BindDropDownList();//載入頁面時,填充第一個下拉列表框
}
}
protected void BindDropDownList()
{
this.Dropdownlist1.Attributes.Add("onchange","javascript:XmlPost(this);"); //伺服器端添加屬性
zlp.str.ReadXmlCS.QueryXml(@"D:\Inetpub\wwwroot\zlp\XmlData\ProductMenu.xml",@"//Pro_class/@id",this.Dropdownlist1);
}

相關詞條

相關搜尋

熱門詞條

聯絡我們