473,403 Members | 2,270 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

How to add Child nodes to Tree view in asp.net dynamically

Hi, all
am using asp.net 2.0, c#
i have a tree view control
from two tables i have to add nodes dynamically
i hav category(cat_id,cat_name),
sub category(sub_cat_id,cat_id,sub_cat_name) tables
under category node i have to fill subcategory names as child node

Expand|Select|Wrap|Line Numbers
  1. con.Open()
  2.             cmd = New SqlCommand("select cat_id,cat_name from tbl_category order by cat_name", con)
  3.             dr = cmd.ExecuteReader()
  4.             While dr.Read()
  5.                 cat = New TreeNode()
  6.                 cat.Text = dr("cat_name").ToString
  7.                 cat.Value = dr("cat_id").ToString
  8.                 TreeView2.Nodes.Add(cat)
  9.             End While
  10.  
  11.             dr.Close()
  12.             con.Close()
using above code i filled parent nodes
how to get child nodes?

please help me
Thankyou
Nov 15 '08 #1
2 41302
Just Copy and paste the below code it will dynamically display the parent node and child nodes, datas will be fetched from the database. You set the tables correctly, hope u add primary key in parent table and foreign key in child table. I added two textbox to enter parent node and child node , users can enter as many child node to a particular parent node separated by commas . and it will display the newly added parent and child node when they click the add button.

Expand|Select|Wrap|Line Numbers
  1.  public partial class NewTreeView : System.Web.UI.Page
  2. {
  3.     SqlConnection con = new SqlConnection("SERVER = \\SQLEXPRESS; DATABASE = db; UID = usr ; PwD = pwd ;");
  4.     int userid = 0;
  5.     string childItem = "";
  6.     protected void Page_Load(object sender, EventArgs e)
  7.     {
  8.         if (!IsPostBack)
  9.         {
  10.           FillTreeView();
  11.         }
  12.     }
  13.     private void FillTreeView()
  14.     {
  15.         try
  16.         {
  17.             con.Open();
  18.             {
  19.                 SqlCommand SqlCmd = new SqlCommand("Select userid ,username from sampledb", con);
  20.                 SqlDataReader Sdr = SqlCmd.ExecuteReader();
  21.                 SqlCmd.Dispose();
  22.                 string[,] ParentNode = new string[100, 2];
  23.                 int count = 0;
  24.  
  25.                 while (Sdr.Read())
  26.                 {
  27.                     ParentNode[count, 0] = Sdr.GetValue(Sdr.GetOrdinal("USERId")).ToString();
  28.                     ParentNode[count++, 1] = Sdr.GetValue(Sdr.GetOrdinal("USERNAME")).ToString();
  29.                 }
  30.                 Sdr.Close();
  31.                 for (int loop = 0; loop < count; loop++)
  32.                 {
  33.                     TreeNode root = new TreeNode();
  34.                     root.Text = ParentNode[loop, 1];
  35.                     root.Target = "_blank";
  36.                     root.NavigateUrl = "Tree1.aspx";
  37.  
  38.                     SqlCommand Module_SqlCmd = new SqlCommand("Select usrid , childname from child where usrid =" + ParentNode[loop, 0], con);
  39.                     SqlDataReader Module_Sdr = Module_SqlCmd.ExecuteReader();
  40.                     while (Module_Sdr.Read())
  41.                     {
  42.                         //siva To Add children module to the root node
  43.                         TreeNode child = new TreeNode();
  44.                         child.Text = Module_Sdr.GetValue(Module_Sdr.GetOrdinal("childname")).ToString();
  45.                         child.Target = "_blank";
  46.                         child.NavigateUrl = "Tree2.aspx";
  47.                         root.ChildNodes.Add(child);
  48.                     }
  49.                     Module_Sdr.Close();
  50.                     TreeView1.Nodes.Add(root);
  51.                     TreeView1.CollapseAll();
  52.  
  53.                 }
  54.  
  55.             }
  56.  
  57.         }
  58.         catch
  59.         {
  60.         }
  61.         finally
  62.         {
  63.             con.Close();
  64.         }
  65.     }
  66.  
  67.       protected void Button2_Click(object sender, EventArgs e)
  68.     {
  69.         con.Open();
  70.         {
  71.            SqlCommand SqlCmd = new SqlCommand("insert into sampledb(USERNAME) values('"+TextBox1.Text+"')", con);
  72.            SqlCmd.ExecuteNonQuery();
  73.            SqlCommand SqlCmd1 = new SqlCommand("SELECT TOP 1 USERID FROM sampledb ORDER BY USERID DESC", con);
  74.            SqlDataReader dr = SqlCmd1.ExecuteReader();
  75.            while (dr.Read())
  76.            {
  77.                userid = Convert.ToInt32(dr["USERID"].ToString());
  78.            }
  79.            dr.Close();
  80.            string childNode = TextBox2.Text;
  81.            string[] childs = new string[3];
  82.            if (childNode.Contains(","))
  83.            {
  84.                childs = childNode.Split(',');
  85.                for (int i = 0; i < childs.Length; i++)
  86.                {
  87.                    childItem = childs[i].ToString();
  88.                    SqlCommand SqlCmd2 = new SqlCommand("insert into child(usrid,childname) values('" + userid + "','" + childItem.ToString() + "')", con);
  89.                    SqlCmd2.ExecuteNonQuery();
  90.                }
  91.  
  92.            }
  93.            else
  94.            {
  95.                SqlCommand SqlCmd3 = new SqlCommand("insert into child(usrid,childname) values('" + userid + "','" + TextBox2.Text + "')", con);
  96.                SqlCmd3.ExecuteNonQuery();
  97.            }
  98.         }
  99.         con.Close();
  100.         Response.Redirect("newtreeview.aspx");
  101.     }
  102.  }
Jun 20 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: imani_technology_spam | last post by:
We need to present hierarchical data on a web page, the same way the tree view shows files in Windows Explorer. Here's the catch: that tree view needs to be bound to a SQL Server database. How...
12
by: Dino L. | last post by:
I am putting data from DataTable to treeView foreach( DataRow aRow in aTable.Rows) { TreeNode tnode = new TreeNode(aRow.ToString() + aRow.ToString() + " " + aRow.ToString());...
3
by: Brian Henry | last post by:
If i already have a tree view created, and want to add another new node to it, how would i do so? Is there a way to throught tags or anything? like i have this RootNode | +-- Child 1 +--...
2
by: Kevin Lippiatt | last post by:
Can anyone advise me on how well the windows .net tree view control performs when loaded with large numbers of nodes. I'm intersted in how it might perform when used with possibly millions of...
0
by: rinishrk | last post by:
Hi, I have a web application that contains subfolders Admin and User with related webpages inside them. Within the Admin folder I have a page containing a Tree View Control.I want this tree view...
0
by: teju | last post by:
Hi all, I am trying to populate tree view from the database. Till two levels i can populate it fine but when it reaches third level it doesn't expand. Below is the code, it has been taken from the...
2
by: Bob | last post by:
Hi, I would to have a tree view control with check boxes in which the parent and child nodes interacts, in the way that you normally see this occuring. Checking the parent node automatically...
0
by: jakki | last post by:
i want to populate a data from a data base.. i wrote a code for webapplication aspx.cs.. which is perfectly work's code is protected void Page_Load(object sender, EventArgs e) {
2
by: Developer111 | last post by:
I am developing a tree view like application using HTML. The problem is that although I am using the same CSS styles for both parent and child node at sub level, but there is an uneven line spacing...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.