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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Vajrala Narendra
    New Member
    • Jun 2007
    • 73

    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_ca t_id,cat_id,sub _cat_name) tables
    under category node i have to fill subcategory names as child node

    Code:
    con.Open()
                cmd = New SqlCommand("select cat_id,cat_name from tbl_category order by cat_name", con)
                dr = cmd.ExecuteReader()
                While dr.Read()
                    cat = New TreeNode()
                    cat.Text = dr("cat_name").ToString
                    cat.Value = dr("cat_id").ToString
                    TreeView2.Nodes.Add(cat)
                End While
    
                dr.Close()
                con.Close()
    using above code i filled parent nodes
    how to get child nodes?

    please help me
    Thankyou
  • sivakumarcse
    New Member
    • Jan 2009
    • 6

    #2
    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.

    Code:
     public partial class NewTreeView : System.Web.UI.Page
    {
        SqlConnection con = new SqlConnection("SERVER = \\SQLEXPRESS; DATABASE = db; UID = usr ; PwD = pwd ;");
        int userid = 0;
        string childItem = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
              FillTreeView();
            }
        }
        private void FillTreeView()
        {
            try
            {
                con.Open();
                {
                    SqlCommand SqlCmd = new SqlCommand("Select userid ,username from sampledb", con);
                    SqlDataReader Sdr = SqlCmd.ExecuteReader();
                    SqlCmd.Dispose();
                    string[,] ParentNode = new string[100, 2];
                    int count = 0;
    
                    while (Sdr.Read())
                    {
                        ParentNode[count, 0] = Sdr.GetValue(Sdr.GetOrdinal("USERId")).ToString();
                        ParentNode[count++, 1] = Sdr.GetValue(Sdr.GetOrdinal("USERNAME")).ToString();
                    }
                    Sdr.Close();
                    for (int loop = 0; loop < count; loop++)
                    {
                        TreeNode root = new TreeNode();
                        root.Text = ParentNode[loop, 1];
                        root.Target = "_blank";
                        root.NavigateUrl = "Tree1.aspx";
    
                        SqlCommand Module_SqlCmd = new SqlCommand("Select usrid , childname from child where usrid =" + ParentNode[loop, 0], con);
                        SqlDataReader Module_Sdr = Module_SqlCmd.ExecuteReader();
                        while (Module_Sdr.Read())
                        {
                            //siva To Add children module to the root node
                            TreeNode child = new TreeNode();
                            child.Text = Module_Sdr.GetValue(Module_Sdr.GetOrdinal("childname")).ToString();
                            child.Target = "_blank";
                            child.NavigateUrl = "Tree2.aspx";
                            root.ChildNodes.Add(child);
                        }
                        Module_Sdr.Close();
                        TreeView1.Nodes.Add(root);
                        TreeView1.CollapseAll();
    
                    }
    
                }
                
            }
            catch
            {
            }
            finally
            {
                con.Close();
            }
        }
    
          protected void Button2_Click(object sender, EventArgs e)
        {
            con.Open();
            {
               SqlCommand SqlCmd = new SqlCommand("insert into sampledb(USERNAME) values('"+TextBox1.Text+"')", con);
               SqlCmd.ExecuteNonQuery();
               SqlCommand SqlCmd1 = new SqlCommand("SELECT TOP 1 USERID FROM sampledb ORDER BY USERID DESC", con);
               SqlDataReader dr = SqlCmd1.ExecuteReader();
               while (dr.Read())
               {
                   userid = Convert.ToInt32(dr["USERID"].ToString());
               }
               dr.Close();
               string childNode = TextBox2.Text;
               string[] childs = new string[3];
               if (childNode.Contains(","))
               {
                   childs = childNode.Split(',');
                   for (int i = 0; i < childs.Length; i++)
                   {
                       childItem = childs[i].ToString();
                       SqlCommand SqlCmd2 = new SqlCommand("insert into child(usrid,childname) values('" + userid + "','" + childItem.ToString() + "')", con);
                       SqlCmd2.ExecuteNonQuery();
                   }
    
               }
               else
               {
                   SqlCommand SqlCmd3 = new SqlCommand("insert into child(usrid,childname) values('" + userid + "','" + TextBox2.Text + "')", con);
                   SqlCmd3.ExecuteNonQuery();
               }
            }
            con.Close();
            Response.Redirect("newtreeview.aspx");
        }
     }

    Comment

    • dilipkakadiya
      New Member
      • Oct 2011
      • 9

      #3
      hope this help

      Comment

      Working...