. 2 Examples of the implementation of infinite classification in net _ practical tips

Source: Internet
Author: User

Always wanted to do this infinite classification, today finally have a good look at the next, found that the principle of implementation is very simple, data structure, with two columns (category number, superior number) can be achieved, but for the convenience of joint inquiries, generally add a column (depth), in this example, I only used two columns, The only thing left is a recursive data binding to the TreeView ~ ~.

Copy Code code 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
{
Child nodes 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 use the TreeView Control for Infinite Classification binding, this time to write a more general and better ~ ~ Hey, the binding dropdownlist~~ thought with the previous log is very close to, but also using recursion, of course, there are many people on the network to add a "Depth (depth)" Of the field, so that when the binding can be more simple AH ~ ~ Of course, there is no need to add, or recursive use up simple AH ~ ~ Not much said, on the code ha:

Copy Code code 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
{
is not a root node
DROPDOWNLIST1.ITEMS.ADD (tmp+ "| |" + dr["ClassName"]. ToString ());
Bind_droplist (dr["ClassId"). ToString (), tmp + "");
}
}
}


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.