TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Vivek Kumar Vishwas
NA
115
56.2k
How to Bind Child Node under Parent Treeview in Asp.net c#
Nov 13 2017 6:37 AM
Hello,
Bellow Is my Requirment.
How to Bind TreeView In asp. Net c# from Database
How to automatically Bind Child Node according to Parent Node and apply expand and collapse.
My code is running successfully but i am unable to add collapse and expand functionality in child node.
Below is my Code.
(aspx)
<asp:TreeView ID="TreeView1" runat="server" ImageSet="XPFileExplorer"
NodeIndent="15" onselectednodechanged="TreeView1_SelectedNodeChanged" >
<HoverNodeStyle Font-Underline="True" ForeColor="#6666AA" />
<NodeStyle Font-Names="Tahoma" Font-Size="8pt" ForeColor="Black" HorizontalPadding="2px"
NodeSpacing="0px" VerticalPadding="2px"></NodeStyle>
<ParentNodeStyle Font-Bold="False" />
<SelectedNodeStyle BackColor="#B5B5B5" Font-Underline="False" HorizontalPadding="0px"
VerticalPadding="0px" />
</asp:TreeView>
2.
(cs)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class CS : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString.ToString());
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = this.GetData("SELECT cid,cname, pid , pname FROM TopParent where pid=10");
this.PopulateTreeView(dt, 0, null);
BindRepeaterRight();
}
}
private void PopulateTreeView(DataTable dtParent, int parentId, TreeNode treeNode)
{
foreach (DataRow row in dtParent.Rows)
{
TreeNode child = new TreeNode
{
Text = row["cName"].ToString(),
Value = row["cId"].ToString()
};
if (parentId == 0)
{
TreeView1.Nodes.Add(child);
DataTable dtChild = this.GetData("select * from TopParent WHERE pid = " + child.Value);
// PopulateTreeView(dtChild, int.Parse(child.Value), child);
Page.ClientScript.RegisterStartupScript(this.GetType(), "ch", "<script>alert('Parent Node Created -" + child.Text + "..')</script>");
if (dtChild.Rows.Count > 0)
{
//........................
int ParentCid = Convert.ToInt32(dtChild.Rows[0]["CID"].ToString());
string query = "select * from TopParent WHERE cid = " + ParentCid;
SqlCommand cmd = new SqlCommand(query, con);
con.Open();
SqlDataReader dr;
dr = cmd.ExecuteReader();
if (dr.Read())
{
String Cname = dr["cname"].ToString();
String cid = dr["cid"].ToString();
DataTable dtnChild = this.GetData("select cid,cname from TopParent WHERE pid = " + cid);
TreeNode nnode = new TreeNode
{
Text = row["cname"].ToString(),
Value = row["cid"].ToString()
};
TreeView1.Nodes.Add(nnode);
PopulateTreeView(dtnChild, int.Parse(cid), nnode);
Page.ClientScript.RegisterStartupScript(this.GetType(), "ch", "<script>alert('Child Node Created -" + Cname + "..')</script>");
}
con.Close();
}
PopulateTreeView(dtChild, int.Parse(child.Value), child);
}
else
{
treeNode.ChildNodes.Add(child);
}
}
}
private DataTable GetData(string query)
{
DataTable dt = new DataTable();
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
sda.SelectCommand = cmd;
sda.Fill(dt);
}
}
return dt;
}
}
}
(3) . Database
USE [TreeView]
GO
/****** Object: Table [dbo].[TopParent] Script Date: 11/09/2017 17:48:18 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[TopParent](
[Id] [int] IDENTITY(1,1) NOT NULL,
[PId] [bigint] NULL,
[PName] [nvarchar](20) NULL,
[Cid] [bigint] NULL,
[CName] [nvarchar](max) NULL
) ON [PRIMARY]
GO
SET IDENTITY_INSERT [dbo].[TopParent] ON
INSERT [dbo].[TopParent] ([PId], [PName], [Cid], [CName]) VALUES ( NULL, NULL, 10, N'India')
INSERT [dbo].[TopParent] ( [PId], [PName], [Cid], [CName]) VALUES ( 10, N'India', 1010, N'Delhi')
INSERT [dbo].[TopParent] ([Id], [PId], [PName], [Cid], [CName]) VALUES (3, 10, N'India', 1011, N'UP')
INSERT [dbo].[TopParent] ([Id], [PId], [PName], [Cid], [CName]) VALUES (4, 10, N'India', 1012, N'MP')
INSERT [dbo].[TopParent] ([Id], [PId], [PName], [Cid], [CName]) VALUES (5, 10, N'India', 1013, N'Haryana')
INSERT [dbo].[TopParent] ([Id], [PId], [PName], [Cid], [CName]) VALUES (6, 1010, N'Delhi', 101010, N'Laxmi Nagar')
INSERT [dbo].[TopParent] ([Id], [PId], [PName], [Cid], [CName]) VALUES (9, 1011, N'UP', 101110, N'Vaishali')
INSERT [dbo].[TopParent] ([Id], [PId], [PName], [Cid], [CName]) VALUES (11, 1012, N'MP', 101210, N'Satna')
INSERT [dbo].[TopParent] ([Id], [PId], [PName], [Cid], [CName]) VALUES (12, 1013, N'Haryana', 101310, N'Rewari')
INSERT [dbo].[TopParent] ([Id], [PId], [PName], [Cid], [CName]) VALUES (7, 1010, N'Delhi', 101011, N'Preet Vihar')
INSERT [dbo].[TopParent] ([Id], [PId], [PName], [Cid], [CName]) VALUES (8, 1010, N'Delhi', 101012, N'Anand Vihar')
INSERT [dbo].[TopParent] ([Id], [PId], [PName], [Cid], [CName]) VALUES (10, 1011, N'UP', 101111, N'Ghaziabad')
SET IDENTITY_INSERT [dbo].[TopParent] OFF
Pleae Solve this .
Thank you
Attachment:
Treeview-in-asp.net.rar
Reply
Answers (
3
)
The remote server returned an error: (500) Internal Server E
how to change frmaework 4.5 to 3.5?