TreeView control is a server side control. This controlis usable for displaying a tree view kind of structure. We can easily display hierarchy and Master/Detail type of data.
Treeview control has following types of nodes:
- Root: A node that has no parent node and one or more child nodes.
- Parent: A node that has a parent node and one or more child nodes.
- Leaf: A node that has no child nodes.
Node is text which displays either selectable text or hyperlinks. Programmatically we can get/set the properties of TreeView. We can display a check box with node text.
Step By Step Implementation
I had used one table to implement TreeView control with Parent and Child with the example of Product Categories and Category Items.
tblProducts Table Structure
- GO
- /****** Object: Table [dbo].[tblProducts] Script Date: 02/19/2016 10:03:02 ******/
- SET ANSI_NULLS ON
- GO
- SET QUOTED_IDENTIFIER ON
- GO
- CREATE TABLE [dbo].[tblProducts](
- [ProductID] [int] IDENTITY(1,1) NOT NULL,
- [ProductName] [nvarchar](50) NOT NULL,
- [ParentID] [int] NULL
- ) ON [PRIMARY]
- Create a new ASP.NET web site EMPTY project.
- Drag and Drop Treeview control on Default.aspx page
- Double click on Web.Config fileand insert connection string.
- <connectionStrings>
- <add name="TVConnectionString" connectionString="Data Source=SAIBABA-PC\SAIBABA;Initial Catalog=MemberCDAC;Integrated Security=True" providerName="System.Data.SqlClient"/>
- </connectionStrings>
Implementation of TreeView with help of three methods: a. FillRootLevel: Root kind of item will populate.
b. FillChildLevel: Root’s child kind of item will popuplate.
c. FillNodes: Fill item in the nodes.
- Fill Root Level
- SqlConnection con = new SqlConnection(constr);
-
-
- string query = "Select pcate.ProductID as ProductCategoryID ,pcate.ProductName as ProductCategoryName, (select count(*) FROM tblProducts " +
- "WHERE parentid=pcate.ProductID) as TotItem From tblProducts pcate where pcate.ParentID Is Null";
- SqlDataAdapter da = new SqlDataAdapter(query, con);
- DataTable dt = new DataTable();
- da.Fill(dt);
- FillNodes(dt, TreeView1.Nodes);
- Fill Child Level
- SqlConnection con = new SqlConnection(constr);
- string query = "Select pcate.ProductID as ProductCategoryID ,pcate.ProductName as ProductCategoryName, (select count(*) FROM tblProducts "+
- " WHERE parentid=pcate.ProductID) as TotItem From tblProducts pcate where pcate.ParentID = " + parentid;
- SqlDataAdapter da = new SqlDataAdapter(query,con);
- DataTable dt = new DataTable();
- da.Fill(dt);
- FillNodes(dt, parentNode.ChildNodes);
- Default.aspx code
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
-
- <!DOCTYPE html>
-
- <html xmlns="http://www.w3.org/1999/xhtml">
-
- <head runat="server">
- <title></title>
- </head>
-
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:TreeView ID="TreeView1" runat="server" ExpandDepth="0" PopulateNodesFromClient="true" ShowLines="true" ShowExpandCollapse="true" OnTreeNodePopulate="TreeView1_TreeNodePopulate">
- </asp:TreeView>
- </div>
- </form>
- </body>
-
- </html>
- Default.aspx.cs code
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Data;
- using System.Data.SqlClient;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
-
- public partial class _Default: System.Web.UI.Page {
- string constr = ConfigurationManager.ConnectionStrings["TVConnectionString"].ConnectionString;
-
- protected void Page_Load(object sender, EventArgs e) {
- if (!IsPostBack) {
- FillRootLevel();
- }
-
- }
-
-
-
-
-
- private void FillRootLevel() {
- SqlConnection con = new SqlConnection(constr);
-
-
- string query = "Select pcate.ProductID as ProductCategoryID ,pcate.ProductName as ProductCategoryName, (select count(*) FROM tblProducts " +
- "WHERE parentid=pcate.ProductID) as TotItem From tblProducts pcate where pcate.ParentID Is Null";
- SqlDataAdapter da = new SqlDataAdapter(query, con);
- DataTable dt = new DataTable();
- da.Fill(dt);
- FillNodes(dt, TreeView1.Nodes);
- }
-
-
-
- private void FillChildLevel(int parentid, TreeNode parentNode) {
- SqlConnection con = new SqlConnection(constr);
- string query = "Select pcate.ProductID as ProductCategoryID ,pcate.ProductName as ProductCategoryName, (select count(*) FROM tblProducts " +
- " WHERE parentid=pcate.ProductID) as TotItem From tblProducts pcate where pcate.ParentID = " + parentid;
- SqlDataAdapter da = new SqlDataAdapter(query, con);
- DataTable dt = new DataTable();
- da.Fill(dt);
- FillNodes(dt, parentNode.ChildNodes);
- }
-
-
-
-
-
-
-
-
- private void FillNodes(DataTable dt, TreeNodeCollection nodes) {
- foreach(DataRow dr in dt.Rows) {
- TreeNode tn = new TreeNode();
- tn.Text = dr["ProductCategoryName"].ToString();
- tn.Value = dr["ProductCategoryID"].ToString();
- nodes.Add(tn);
-
-
- tn.PopulateOnDemand = (Convert.ToInt32(dr["TotItem"]) > 0);
- }
- }
-
-
- protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e) {
- FillChildLevel(Convert.ToInt32(e.Node.Value), e.Node);
- }
-
- }
- Root level
- Expanded Level : TreeView