469,366 Members | 2,311 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,366 developers. It's quick & easy.

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 40070
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

Post your reply

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

Similar topics

3 posts views Thread by imani_technology_spam | last post: by
12 posts views Thread by Dino L. | last post: by
2 posts views Thread by Kevin Lippiatt | last post: by
reply views Thread by jakki | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.