Two examples of implementing unlimited classification in. Net and. net unlimited Classification

Source: Internet
Author: User

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 + "");
}
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.