How to use TreeView in ASP.NET
This article mainly explains "how to use TreeView in ASP.NET". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to use TreeView in ASP.NET.
The copy 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)
{
}
/ / first-level 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
}
}
}
/ / second-level 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
}
}
}
/ / 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
}
}
}
}
Thank you for reading, the above is the content of "how to use TreeView in ASP.NET", after the study of this article, I believe you have a deeper understanding of how to use TreeView in ASP.NET, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!