请输入您要查询的百科知识:

 

词条 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);

}

随便看

 

百科全书收录594082条中文百科知识,基本涵盖了大多数领域的百科知识,是一部内容开放、自由的电子版百科全书。

 

Copyright © 2004-2023 Cnenc.net All Rights Reserved
更新时间:2024/12/19 7:20:52