request|xml|xmlhttprequest| Refresh | no Refresh | dropdown | drop down list
Two-level linkage Drop-down list, from the idea, roughly divided into the following steps:
One: Triggers the onchange event for the first Drop-down list.
Two: After the processing of the page asynchronously, the information is returned after the request is processed (the server side writes the string to be returned with the Response.Write method, and the client receives the string with the ResponseText property).
Third: Update the second Drop-down list box with the resulting string
The main code is as follows:
One: Create a class that reads XML, which can also be a database query
public class Readxmlcs
{
Public const string k = "|";
Public Const string s = ",";
Public Readxmlcs ()
{
//
TODO: Add constructor logic here
//
}
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 () + ",");//Add the attribute value to the end of the string, add the grouping character ","
System.Xml.XPath.XPathNavigator MyXPathNavigator2 = MyXPathNodeIterator.Current.Clone ()//clone current Contact
while (Myxpathnavigator2.movetonextattribute ())//Move pointer to next property
// {
Sb. Append (myXPathNavigator2.Value.ToString () + "|"); /Add the attribute value to the end of the string and add the grouping character "|"
// }
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 ();//Add the property value (ID) to the ListItem Value property
System.Xml.XPath.XPathNavigator MyXPathNavigator2 = MyXPathNodeIterator.Current.Clone ()//clone current Contact
while (Myxpathnavigator2.movetonextattribute ())//Move pointer to next property
// {
Li. Text = myXPathNavigator2.Value.ToString ()//Add the property value (title) to the ListItem Text property
// }
Myxpathnavigator2.movetonextattribute ();
Li. Text = MyXPathNavigator2.Value.ToString ();
Dd. Items.Add (LI);
}
}
catch{}
}
}
The key steps are annotated and others can be found on MSDN.
Two. Implementation (WebForm8.aspx.cs page):
protected System.Web.UI.WebControls.DropDownList DropDownList2;
protected System.Web.UI.WebControls.DropDownList Dropdownlist1;
private void Page_Load (object sender, System.EventArgs e)
{
Place user code here to initialize page
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 (), fill the first Drop-down list box when the page is loaded
}
}
protected void Binddropdownlist ()
{
This. DROPDOWNLIST1.ATTRIBUTES.ADD ("onchange", "javascript:xmlpost (this);"); Server-Side Add properties
ZLP.STR.READXMLCS.QUERYXML (@ "D:\Inetpub\wwwroot\zlp\XmlData\ProductMenu.xml", @ "//pro_class/@id", this. DROPDOWNLIST1);
}
Three: JS script (Webform8.aspx page)
Do not know why, here's JS script added not to come in, you can download the case to get the source code.
End of article:
You can also add it to a user control or encapsulate it as a custom control.
If we have better methods and suggestions, please inform me, welcome correction, Thank you!
qq:126083810
Zlp8383178@163.com
Example Demo:
Http://j.thec.cn/zlp8383178/exmDropDownList/webform1.aspx
Case download