Two examples of implementing unlimited classification in. Net and. net unlimited Classification
I used to think about this infinite classification. Today I finally had a good look at it. I found that the implementation principle is still very simple. In the data structure, I used two columns (Classification Number, Superior Number) this can be achieved, but for the convenience of joint queries, a column (depth) is usually added. In this instance, I only use two columns, the rest is nothing more than recursively Binding data to the TreeView ~~.
Copy codeThe Code is as follows:
Public partial class _ Default: System. Web. UI. Page
{
BIL bil = new BIL ();
Protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack)
{
Bind_tree ("0", null );
}
}
Protected void bind_tree (string ChildNode, TreeNode tn)
{
DataTable dt = bil. GetByClassPre (ChildNode). Tables [0];
Foreach (DataRow dr in dt. Rows)
{
TreeNode Node = new TreeNode ();
If (tn = null)
{
// Root
Node. Text = dr ["ClassName"]. ToString ();
This. TreeView1.Nodes. Add (Node );
Bind_tree (dr ["ClassId"]. ToString (), Node );
}
Else
{
// Subnode of the current node
Node. Text = dr ["ClassName"]. ToString ();
Tn. ChildNodes. Add (Node );
Bind_tree (dr ["ClassId"]. ToString (), Node );
}
}
}
}
The last time I wrote a method to bind infinite categories using the TreeView control, this time I wrote a more general ~~ Hey, bind DropDownList ~~ The idea is very similar to the previous log and uses recursion. Of course, there are still many people on the network who add a "Depth (Depth)" field to the database, this makes binding easier ~~ Of course, there is no need to add it. It's easier to use it recursively ~~ Let's not talk about it anymore. Let's go to the Code:
Copy codeThe Code is as follows:
Protected void bind_droplist (string ChildNode, string tmp)
{
DataTable dt = bil. GetByClassPre (ChildNode). Tables [0];
Foreach (DataRow dr in dt. Rows)
{
If (dr ["ClassPre"]. ToString () = "0 ")
{
// If it is a root node
Tmp = "";
DropDownList1.Items. Add (dr ["ClassName"]. ToString ());
Bind_droplist (dr ["ClassId"]. ToString (), tmp + "");
}
Else
{
// Not the root node
DropDownList1.Items. Add (tmp + "|-" + dr ["ClassName"]. ToString ());
Bind_droplist (dr ["ClassId"]. ToString (), tmp + "");
}
}
}