Copy Code code as follows:
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Data.Linq;
Using System.Xml;
Using System.Xml.Linq;
Namespace WebApplication3 {
Public partial class WebForm1:System.Web.UI.Page {
protected void Page_Load (object sender, EventArgs e) {
int id = convert.toint32 (request["ParentID"]);
GetXML (ID);
}
Public IList < Product > getlist () {
return new List < Product > () {
New Product () {
Id = 1,
ParentID = 0,
Haschild = 1,
Name = "AAAAA"
},
New Product () {
Id = 2,
ParentID = 1,
Haschild = 1,
Name = "BBBB1"
},
New Product () {
Id = 3,
ParentID = 2,
Haschild = 0,
Name = "CCCCC2"
},
New Product () {
Id = 4,
ParentID = 2,
Haschild = 0,
Name = "Ddddd3"
},
New Product () {
Id = 5,
ParentID = 1,
Haschild = 0,
Name = "Eeeeee4"
},
New Product () {
Id = 6,
ParentID = 3,
Haschild = 0,
Name = "Ffffff5"
},
New Product () {
Id = 7,
ParentID = 4,
Haschild = 0,
Name = "Ggggggg6"
},
New Product () {
Id = 8,
ParentID = 7,
Haschild = 0,
Name = "Hhhhhhh7"
},
New Product () {
Id = 9,
ParentID = 0,
Haschild = 0,
Name = "Jjjjjjj8"
},
New Product () {
Id = 10,
ParentID = 0,
Haschild = 0,
Name = "Yyyyyyyy9"
}
};
}///<summary>
Read child nodes through parent node and splice into XML to foreground
</summary>
<param name= "ParentID" ></param>
public void GetXML (int parentid) {
list<product> list = GetList (). Where (x => x.parentid = = ParentID). ToList ();
XElement XElement = new XElement ("Texttree");
foreach (Product p in list) {
Xelement.add (New XElement ("value", New XAttribute ("id", p.id), p.name));
}
Xelement.save ("D:\\kissnana.xml");
XmlDocument xdocument = new XmlDocument ();
XDocument. Load ("D:\\kissnana.xml");
Response.ContentType = "Text/xml";
XDocument. Save (Response.outputstream);
Response.End ();
}
}
public class Product {
public int id{set;get;}
public int parentid{set;get;}
public int haschild{set;get;}
public string Name{set;get;}
}}
The idea is very simple, backstage use XML sent to the foreground through jquery receive processing mosaic UL,
Li principle (using < li > nested < ul >, local read all the subordinate nodes under a node, each click Read, read, then enter the Getdisplayornot () method to determine the display and hidden nodes) HTML code: < Body > <form id = "Form1"
runat = "server" > <input type = "button"
Value = "Text"
onclick = "Loadxml (0)"/><div id = "root" > </div>
</form >
</body>
Foreground code:
Copy Code code as follows:
<script type= "Text/javascript" >
var object1 = null;
function Loadxml (ID) {
$.ajax ({
Type: "
Post ",
URL: "
WebForm1.aspx? ParentID = "+id,
DataType: "
XML ",
Success:createtree
});
}
The idea is that each parent node produces a UL child and Ajax reads the father's direct son, the mosaic tree
function Createtree (XML) {
var obj = Object1 = null? ("#root"): (Object1);//judgment is not the first load, if the first load is the most external div ID, otherwise is the parent node
$ (obj). Append ("< ul class = ' Ulfather ' >");//Add UL Object
$ (XML). Find ("
Value "). each (function () {/) reads all the value node information from the XML and gets the current hierarchy node
Stitching <li> and <a> properties are spliced through the attribute ID and node text of the value node in XML
$ (obj). Children (". Ulfather "). Append (" < li > <a id = "+ $ (This). attr ("
ID ") +" > "+ $ (This). Text () +" </a></li > ");
var ALink = "#" + $ (This). attr ("
ID "); Get the current Hyperlink object
$ (ALink). Bind ("
Click, function () {//Hyperlink binding fixed-point hit Event
if ($ (ALink + "+ ul"). Size () <= 0) {//If the data is already bound, you do not need to bind again, (if the next element of the hyperlink is UL, the data has been tied)
Object1 = $ (ALink). Parent ("
Li ");
Loadxml ($ (this). attr ("
ID "))
}
else {
Getdisplayornot ($ (ALink));
}
});
});
}
Node Display or shadow hiding
function Getdisplayornot (obj) {
if ($ (obj). Parent ("
Li "). Children ("
UL "). Is (": hidden ")) {
$ (obj). Parent ("
Li "). Children ("
UL "). CSS ("
Display ","
Block ");
}
else {
$ (obj). Parent ("
Li "). Children ("
UL "). CSS ("
Display ","
None ");
}
}
</script>
Background:
Copy Code code as follows:
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Data.Linq;
Using System.Xml;
Using System.Xml.Linq;
Namespace WebApplication3
{
public partial class WebForm1:System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
int ID =convert.toint32 (request["ParentID"]);
GetXML (ID);
}
Public ilist<product> getlist ()
{
return new list<product> ()
{
New Product () {id=1, parentid=0, haschild=1, name= "AAAAA"},
New Product () {id=2, parentid=1, haschild=1, name= "BBBB1"},
New Product () {id=3, parentid=2, Haschild=0, name= "CCCCC2"},
New Product () {id=4, parentid=2, Haschild=0, name= "Ddddd3"},
New Product () {id=5, parentid=1, Haschild=0, name= "Eeeeee4"},
New Product () {id=6, parentid=3, Haschild=0, name= "Ffffff5"},
New Product () {id=7, parentid=4, Haschild=0, name= "Ggggggg6"},
New Product () {id=8, parentid=7, Haschild=0, name= "Hhhhhhh7"},
New Product () {id=9, parentid=0, Haschild=0, name= "Jjjjjjj8"},
New Product () {id=10,parentid=0, haschild=0, name= "Yyyyyyyy9"}
};
}
<summary>
Read child nodes through parent node and splice into XML to foreground
</summary>
<param name= "ParentID" ></param>
public void GetXML (int parentid)
{
list<product> list = GetList (). Where (x => x.parentid = = ParentID). ToList ();
XElement XElement = new XElement ("Texttree");
foreach (Product p in list)
{
Xelement.add (New XElement ("value", New XAttribute ("id", p.id), p.name));
}
Xelement.save ("D:\\kissnana.xml");
XmlDocument xdocument = new XmlDocument ();
XDocument. Load ("D:\\kissnana.xml");
Response.ContentType = "Text/xml";
XDocument. Save (Response.outputstream);
Response.End ();
}
}
public class Product
{
public int id{set;get;}
public int parentid{set;get;}
public int haschild{set;get;}
public string Name{set;get;}
}
}