Use ASP.NET TreeView control

<asp:TreeView ID="TreeView2" runat="server"></asp:TreeView>

database:

 

 Front Code:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="TreeView.index" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TreeView ID="TreeView1" runat="server">
<Nodes>
<asp:TreeNode Text="新建节点" Value="新建节点">
<asp:TreeNode Text="新建节点" Value="新建节点"></ ASP: the TreeNode> </ Nodes></ ASP: the TreeNode><ASP: the TreeNode the Text = "new node" Value = "New Node"> </ asp: TreeNode ><ASP: the TreeNode the Text = "new node" Value = "New Node">
</ ASP: the TreeNode>




</ ASP: the TreeView>
<H2> The above is the view edit, the following is the code to add </ H2>
<ASP: the TreeView ID = "TreeViewdb" the runat = "Server"> </ ASP: the TreeView>
<H2> The following is Add code </ h2> database
<ASP: the TreeView ID = "TreeView2" the runat = "Server"> </ ASP: the TreeView>
</ div>
</ form>
</ body>
</ HTML>

 

Background Code:


the System the using;
the using the System.Collections.Generic;
the using the System.Linq;
the using the System.Web;
the using System.Web.UI;
the using the System.Web.UI.WebControls;



the using the System.Data;
the using the BLL;
namespace the TreeView
{
public partial class index: System.Web.UI.Page
{
protected void the Page_Load (Object SENDER, EventArgs E)
{
IF (the Page.IsPostBack!)
{
this.TreeViewdb.Dispose (); // cleanup
TreeNodeCollection tnc = this.TreeViewdb.Nodes; / / root node represents a collection of
the TreeNode tnNodel the TreeNode new new = ();
tnNodel.Text = "this is a root";
tnc.Add (tnNodel);
the TreeNode the TreeNode new new T2 = ();
t2.Text = "this is the first two root ";
tnc.Add (T2);
//
the TreeNode tnNode1 the TreeNode new new = ();
tnNode1.Text = "This is a child node of the root node";
tnNodel.ChildNodes.Add (tnNode1);
/ *************** ***************************************
* the DataSet DataTable DataView
* are two-dimensional table , DataSet is out of the database query result set
* can multiple tables (return multiple tables), is .Table [ "table name"] or .Table [ "0"] or .Table [ "1"]
* general Whether it is a single-table queries or multi-table queries final result is a table so we .Table [ "0"] is enough to use
* DataTable DTB = dataset.table [ "0"]
* DataView .table = DTB
* About .RowFilter = "string", here is the part of the filter, there are conditions like SQL queries
*
* *************************** ******************* /

///////
InitTree (this.TreeView2.Nodes, "0");
}
}
/// <Summary>
/// recursive initialization the TreeView
/// </ Summary>
/// <param name = "tnc" > set of nodes </ param>
/// <param name = "parentId"> parent node ID </ param>
Private void InitTree (TreeNodeCollection TNC, String parentId)
{
the TreeNode Node;
the DataView new new DV = the DataView ();
// bind (DataSe in the Table [0] bound to the DataView, screening)
dv.Table BLL.UserBLL.allNodes = ();
// filter out the corresponding attribute, ( in line with the parent node id), and the rest removed.
= dv.RowFilter "ParentNodeId =" + parentId; //
the foreach (DRV in the DataRowView DV)
{
// the DataRowView line
Node the TreeNode new new = ();
node.Value DRV = [ "the NodeId"] the ToString ();.
Node.text DRV = [ "the DisplayName"] the ToString ();.
// add the new node to set up the delivery node
tnc.Add (node);
// recursive
InitTree (node.ChildNodes,

}
}
}

 

SysFun.cs (Model)

using System;
using System.Collections.Generic;
using System.Text;

namespace Model
{
/// <summary>
/// TreeView的结点 类
/// </summary>
public class SysFun
{
private int nodeId;
private string displayName;
private string nodeUrl;
private string parentNodeId;

public int NodeId { get => nodeId; set => nodeId = value; }
public string DisplayName { get => displayName; set => displayName = value; }
public string NodeUrl { get => nodeUrl; set => nodeUrl = value; }
public string ParentNodeId { get => parentNodeId; set => parentNodeId = value; }
}
}

 

 

 

 

----------------
Disclaimer: This article is the original article CSDN bloggers "Han Han YH", the follow CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source and link this statement.
Original link: https: //blog.csdn.net/u012997311/article/details/78404483

Guess you like

Origin www.cnblogs.com/ljs-13/p/12157933.html