Click here to Skip to main content
16,017,788 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Problem is when i expand selected node it will collapse or expand all other nodes


What I have tried:

protected void Page_Load(object sender, EventArgs e)
       {
           if (!IsPostBack)
           {
               GetTreeViewItems();



               if (Session["SelNodVal"] != null)
               {

               }
           }
       }

       private void GetTreeViewItems()
       {
           string cs = ConfigurationManager.ConnectionStrings["Data"].ConnectionString;
           SqlConnection con = new SqlConnection(cs);
           SqlDataAdapter da = new SqlDataAdapter("spGetTreeViewItems", con);
           DataSet ds = new DataSet();
           da.Fill(ds);
           ds.Relations.Add("ChildRows", ds.Tables[0].Columns["ID"],
               ds.Tables[0].Columns["ParentId"]);

           foreach (DataRow level1DataRow in ds.Tables[0].Rows)
           {
               if (string.IsNullOrEmpty(level1DataRow["ParentId"].ToString()))
               {
                   TreeNode parentTreeNode = new TreeNode();
                   parentTreeNode.Text = level1DataRow["Name"].ToString();
                   //Session["Name"] = parentTreeNode.Text;

                   // parentTreeNode.NavigateUrl = level1DataRow["URL"].ToString();

                   //parentTreeNode.Expand();
                   //Session["URL"] = parentTreeNode.NavigateUrl;
                   //  parentTreeNode.Collapse();
                   //if (Session["SelNodVal"]== TreeView1.SelectedNode)
                   //{
                   //    TreeView1.SelectedNode.Expanded=true;
                   //}


                   GetChildRows(level1DataRow, parentTreeNode);
                   TreeView1.Nodes.Add(parentTreeNode);
               }
           }
       }
       private void GetChildRows(DataRow dataRow, TreeNode treeNode)
       {
           DataRow[] childRows = dataRow.GetChildRows("ChildRows");

           foreach (DataRow row in childRows)
           {
               TreeNode childTreeNode = new TreeNode();
               childTreeNode.Text = row["Name"].ToString();

               //if (Session["SelNodVal"] != null)
               //{

               //    childTreeNode.Value = Session["SelNodVal"].ToString();
               //    childTreeNode.Expanded = true;
               //}
               //childTreeNode.NavigateUrl = row["URL"].ToString();

               treeNode.ChildNodes.Add(childTreeNode);
               //       childTreeNode.Collapse();
               if (row.GetChildRows("ChildRows").Length > 0)
               {
                   GetChildRows(row, childTreeNode);

               }
           }
       }
       protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
       {
           String h = "";
           String u = "";
           String st = TreeView1.SelectedNode.Value;
           string cs = ConfigurationManager.ConnectionStrings["Data"].ConnectionString;
           SqlConnection con = new SqlConnection(cs);
           String qry = "select ID,URL from [vehicle].[dbo].[tblTreeViewItems] where Name = '" + st + "'";
           SqlDataAdapter da = new SqlDataAdapter(qry, con);
           DataSet ds = new DataSet();
           da.Fill(ds);
           if (ds.Tables[0].Rows.Count > 0)
           {
               h = ds.Tables[0].Rows[0]["ID"].ToString();
               u = ds.Tables[0].Rows[0]["URL"].ToString();
           }
           Session["SelNodVal"] = st;
           Response.Redirect(u);
       }

       }
   }
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900