A small example of TreeView usage in ASP. NET

Source: Internet
Author: User

Copy codeThe Code is as follows:
Using System;
Using System. Collections;
Using System. Configuration;
Using System. Data;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. HtmlControls;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Data. SqlClient;
Using Common;
Using DataAccess;

Public partial class Controls_ProductType2: System. Web. UI. UserControl
{


ProductType productType = new ProductType ();
Product product = new Product ();

Protected void TreeView_TreeNodePopulate (object sender, TreeNodeEventArgs e)
{

If (e. Node. ChildNodes. Count = 0)
{
Switch (e. Node. Depth)
{
Case 0:
PopulateCategories (e. Node );
Break;
Case 1:
PopulateProductType (e. Node );
Break;
Case 2:
PopulateProducts (e. Node );
Break;
}
}

// Int I = TreeToXml. TreeToXML (TreeView1, "D:/TreeToXML. xml ");

}

Protected void Page_Load (object sender, EventArgs e)
{

}

// Level 1 node (parentID = 0)
Void PopulateCategories (TreeNode node)
{
DataTable dt = new DataTable ();
Dt = productType. GetListByFatherNode ();

If (dt. Rows. Count> 0)
{
Foreach (DataRow row in dt. Rows)
{
TreeNode NewNode = new TreeNode (row ["ProductTypeName"]. ToString (), row ["ProductTypeID"]. ToString (), "", "../WebProductList. aspx? ProductTypeIdOne = "+ row [" ProductTypeID "]. ToString () + "","");
NewNode. PopulateOnDemand = true;
NewNode. SelectAction = TreeNodeSelectAction. Expand;
Node. ChildNodes. Add (NewNode );
Node. Expanded = true;
}
}

}

// Level 2 node
Void PopulateProductType (TreeNode node)
{
DataTable dt = new DataTable ();
QueryParam queryparam = new QueryParam ();
Queryparam. Add ("ParentID", QueryParam. RELATION. EQ, "" + node. Value + "");
Dt = productType. GetListByBranchNode (queryparam );

If (dt. Rows. Count> 0)
{
Foreach (DataRow row in dt. Rows)
{
TreeNode NewNode = new TreeNode (row ["ProductTypeName"]. ToString (), row ["ProductTypeID"]. ToString (), "", "../WebProductList. aspx? ProductTypeIdTwo = "+ row [" ProductTypeID "]. ToString () + "","");
NewNode. PopulateOnDemand = true;
NewNode. SelectAction = TreeNodeSelectAction. Expand;
Node. ChildNodes. Add (NewNode );
Node. Expanded = false;
}
}
}

// The third Node
Void PopulateProducts (TreeNode node)
{
DataTable dt = new DataTable ();
QueryParam queryparam = new QueryParam ();
Queryparam. Add ("ProductTypeItemID", QueryParam. RELATION. EQ, "" + node. Value + "");
Dt = product. GetList (queryparam );

If (dt. Rows. Count> 0)
{
Foreach (DataRow row in dt. Rows)
{
TreeNode NewNode = new TreeNode (row ["ProductName"]. ToString (), row ["ProductID"]. ToString (), "", "../WebProductDetaile. aspx? ProductID = "+ row [" ProductID "]. ToString () + "","");
// NewNode. PopulateOnDemand = false;
// NewNode. SelectAction = TreeNodeSelectAction. None;
NewNode. PopulateOnDemand = true;
NewNode. SelectAction = TreeNodeSelectAction. Expand;
Node. ChildNodes. Add (NewNode );
Node. Expanded = false;
}
}
}


}

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.