473,769 Members | 2,077 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

73 New Member
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_ca t_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 41356
sivakumarcse
6 New Member
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
10825
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 can this be done?
12
20532
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()); treeView1.Nodes.Add(tnode); //till here code works fine //now I wanna add child nodes for last inserted node foreach( DataRow GrupaRow in TabelaGrupe.Rows)
3
2488
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 +-- Child 2 and i want to add a child node to child 1 how would i refrence it and add a
2
1690
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 nodes. At what point will the control be 'over-stressed'?
0
1432
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 to have nodes for all pages in the website. However it seems that I cannot create child nodes within this tree view for pages that exist outside the Admin folder.(i.e.pags within the User folder or the Root folder ).It gives me an errors stating...
0
1728
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 reference of micrososft Sub PopulateNode(ByVal sender As Object, ByVal e As TreeNodeEventArgs) ' Call the appropriate method to populate a node at a particular level. If e.Node.ChildNodes.Count = 0 Then Select Case...
2
4171
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 checks all its children node. Conversely, if one of the child nodes is unchecked, the parent node unchecks. Is there a property of the treeview that will automatically do this for me or do I need to roll my own? Thanks, Bob
0
1077
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
2501
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 between parent and child nodes, when parent node in collapsed i.e. EXPANDED TREE STATE:- -Parent Node -Sub-Parent Node. -Child 1 of Sub-Parent Node. . Desc of Child 1 -Child 2 of Sub-Parent Node. . Desc of Child 2
0
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10215
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10049
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9996
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9865
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7410
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6674
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3964
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.