Click here to Skip to main content
15,896,339 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

I have a tree(tree view) where nodes are added dynamically from database.
Now i want to open different pages on those node click at runtime on TreeView_SelectedNodeChanged Event.
How can i manage that in that tree.

This is XML code for treeview
XML
<asp:TreeView ID="TreeView1" runat="server"
                    ImageSet="BulletedList2" ShowLines="True" CssClass="container" ExpandDepth="1" OnSelectedNodeChanged="TreeView1_SelectedNodeChanged">
                    <ParentNodeStyle Font-Bold="False" />
                    <HoverNodeStyle Font-Underline="True" ForeColor="#5555DD" />
                    <SelectedNodeStyle Font-Underline="True" ForeColor="#5555DD"
                        HorizontalPadding="0px" VerticalPadding="0px" />
                    <NodeStyle Font-Names="Verdana" Font-Size="8pt" ForeColor="Black"
                        HorizontalPadding="5px" NodeSpacing="0px" VerticalPadding="0px" />
</asp:TreeView>


This codebehind in C# which generates treenode dynamically
C#
private void LoadData()
  {
      con = new SqlConnection(WebConfigurationManager.ConnectionStrings["ConString"].ConnectionString);
      cmd = new SqlCommand("SELECT Project_Code, Project_Name FROM tablename WHERE Project_Id=1", con);                // WHERE Project_Id=1
      con.Open();
      reader = cmd.ExecuteReader();

      arrCountry = new string[100, 2];
      int ctr = 0;
      while (reader.Read())
      {
          arrCountry[ctr, 0] = reader["Project_Code"].ToString();
          arrCountry[ctr, 1] = reader["Project_Name"].ToString();
          ctr++;
      }
      reader.Close();
      con.Close();

      for (int i = 0; i < ctr; i++)
      {
          TreeNode Country = new TreeNode();
          Country.Value = arrCountry[i, 0];
          Country.Text = arrCountry[i, 1];

          cmd = new SqlCommand("SELECT Design_Code, Design_Name FROM tablename1 WHERE Project_Code=@Project_Code", con);
          cmd.Parameters.AddWithValue("@Project_Code", arrCountry[i, 0]);
          con.Open();
          reader = cmd.ExecuteReader();
          arrState = new string[100, 2];
          int ctr2 = 0;
          while (reader.Read())
          {
              arrState[ctr2, 0] = reader["Design_Code"].ToString();
              arrState[ctr2, 1] = reader["Design_Name"].ToString();
              ctr2++;
          }
          reader.Close();
          con.Close();
          for (int j = 0; j < ctr2; j++)
          {
              TreeNode state = new TreeNode();
              state.Value = arrState[j, 0];
              state.Text = arrState[j, 1];

              Country.ChildNodes.Add(state);

              cmd = new SqlCommand("SELECT Shell_Code, Shell_Name FROM tblShellDetails WHERE Design_Code=@Design_Code", con);
              cmd.Parameters.AddWithValue("@Design_Code", arrState[j, 0]);
              con.Open();
              reader = cmd.ExecuteReader();
              arrCity = new string[100, 2];
              int ctr3 = 0;
              while (reader.Read())
              {
                  arrCity[ctr3, 0] = reader["Shell_Code"].ToString();
                  arrCity[ctr3, 1] = reader["Shell_Name"].ToString();

                  ctr3++;
              }
              reader.Close();
              con.Close();

              for (int l = 0; l < ctr3; l++)
              {
                  TreeNode city = new TreeNode();
                  city.Value = arrCity[l, 0];
                  city.Text = arrCity[l, 1];

                  state.ChildNodes.Add(city);

                  cmd = new SqlCommand("SELECT Shell_Code, Shell_Name FROM tablename3 WHERE Shell_Code=@Shell_Code", con);
                  cmd.Parameters.AddWithValue("@Shell_Code", arrCity[l, 0]);
                  con.Open();
                  reader = cmd.ExecuteReader();
                  arrabc = new string[100, 2];
                  int ctr4 = 0;
                  while (reader.Read())
                  {
                      arrabc[ctr4, 0] = reader["Shell_Code"].ToString();
                      arrabc[ctr4, 1] = reader["Shell_Name"].ToString();

                      ctr4++;
                  }
                  reader.Close();
                  con.Close();

                  for (int m = 0; m < ctr4; m++)
                  {
                      TreeNode abc = new TreeNode(arrabc[m, 1], arrabc[m, 0]);
                      city.ChildNodes.Add(abc);
                  }
              }
          }
          // TreeView tv = (TreeView)Master.FindControl("TreeView");
          TreeView1.Nodes.Add(Country);
      }
  }


Thanks for your help
Posted

1 solution

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