CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jan 2009
    Posts
    22

    dynamic treeview problem

    I have a treeview control in ASP.NET and C#.

    Root node This is fixed
    ---Parent Node 1 Parent node and child are populated from the database directly.
    ----Child node1
    ----Child node2
    ---Parent node 2

    Now When a value is added to a database it get added in the treeview.
    I cannot get a way to select the parent and child node and make it perform a function like go to another page or something.....

    Please help me.... urgent

  2. #2
    Join Date
    Jan 2009
    Posts
    22

    Re: dynamic treeview problem

    I have the code to retrieve values from the database and display on the treenode dynamically.
    Just the selection is a problem.

    If there is a tutorial of any other information please let me kow

  3. #3
    Join Date
    Jan 2009
    Posts
    22

    Re: dynamic treeview problem

    Is there no 1 who knows the answer to this?/?
    Even how to go about information will do.

  4. #4
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Re: dynamic treeview problem

    I cannot get a way to select the parent and child node and make it perform a function like go to another page or something.....
    Can you explain in a bit more detail what you mean by this statement.
    Are you selecting two nodes at a time?
    What happens when you click on the node(s), ie, which bit 'does not work'?
    What do you mean by "a new page or something"?

    If you answer these questions, maybe there is somebody who can help you.
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

  5. #5
    Join Date
    Jan 2009
    Posts
    22

    Re: dynamic treeview problem

    No, I am selecting only one node i.e. the parent node or a child node.
    But since the treenodes are being populated from the database i dont know how to generate an event when a particular node is selected.
    Here when a particular node is clicked it goes to another page along with the value clicked and displays it in a label on that page. (so basically i am passing values from one page to the other).

  6. #6
    Join Date
    Jan 2009
    Posts
    22

    Re: dynamic treeview problem

    the code to populate the tree node:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.Data.SqlClient;

    namespace WebApplication2
    {
    public partial class treedatabase : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void TreeView2_TreeNodePopulate(object sender, TreeNodeEventArgs e)
    {
    if (e.Node.ChildNodes.Count == 0)
    {
    switch (e.Node.Depth)
    {
    case 0:
    PopulateMachineGroups(e.Node);
    break;
    case 1:
    PopulateMachines(e.Node);
    break;

    }
    }
    }

    protected void TreeView3_TreeNodePopulate(object sender, TreeNodeEventArgs e)
    {
    if (e.Node.ChildNodes.Count == 0)
    {
    switch (e.Node.Depth)
    {
    case 0:
    PopulatePolicies(e.Node);
    break;

    }
    }
    }
    # region Policies
    private void PopulatePolicies(TreeNode treeNode)
    {
    SqlCommand sqlQuery = new SqlCommand(
    "Select PolicyName, PolicyID From Policies");
    DataSet resultSet;
    resultSet = RunQuery(sqlQuery);
    if (resultSet.Tables.Count > 0)
    {
    foreach (DataRow row in resultSet.Tables[0].Rows)
    {
    TreeNode NewNode = new
    TreeNode(row["PolicyName"].ToString(),
    row["PolicyID"].ToString());
    NewNode.PopulateOnDemand = true;
    NewNode.SelectAction = TreeNodeSelectAction.Expand;
    treeNode.ChildNodes.Add(NewNode);
    }
    }
    }
    # endregion
    # region MAchineGroups
    private void PopulateMachineGroups(TreeNode treeNode)
    {
    SqlCommand sqlQuery = new SqlCommand(
    "Select MachineGroupName, MachineGroupID From MachineGroups");
    DataSet resultSet;
    resultSet = RunQuery(sqlQuery);
    if (resultSet.Tables.Count > 0)
    {
    foreach (DataRow row in resultSet.Tables[0].Rows)
    {
    TreeNode NewNode = new
    TreeNode(row["MachineGroupName"].ToString(),
    row["MachineGroupID"].ToString());

    NewNode.PopulateOnDemand = true;
    NewNode.SelectAction = TreeNodeSelectAction.Expand;
    treeNode.ChildNodes.Add(NewNode);
    }
    }
    }
    # endregion
    private void PopulateMachines(TreeNode node)
    {
    SqlCommand sqlQuery = new SqlCommand();
    sqlQuery.CommandText = "Select MachineName From Machines " +
    " Where MachineGroupID = @machinegrpid";
    sqlQuery.Parameters.Add("@machinegrpid", SqlDbType.Int).Value =node.Value;
    DataSet ResultSet = RunQuery(sqlQuery);
    if (ResultSet.Tables.Count > 0)
    {
    foreach (DataRow row in ResultSet.Tables[0].Rows)
    {
    TreeNode NewNode = new
    TreeNode(row["MachineName"].ToString());

    NewNode.PopulateOnDemand = false;
    NewNode.SelectAction = TreeNodeSelectAction.Select;
    node.ChildNodes.Add(NewNode);
    }
    }
    }

    private DataSet RunQuery(SqlCommand sqlQuery)
    {
    SqlConnection DBConnection = new SqlConnection(@"Data Source=JAGMIT-PC\SQLEXPRESS;Initial Catalog=SumooHAgentDB;Integrated Security=True");
    SqlDataAdapter dbAdapter = new SqlDataAdapter();
    dbAdapter.SelectCommand = sqlQuery;
    sqlQuery.Connection = DBConnection;
    DataSet resultsDataSet = new DataSet();
    try
    {
    dbAdapter.Fill(resultsDataSet);
    }
    catch
    {
    // labelStatus.Text = "Unable to connect to SQL Server.";
    }
    return resultsDataSet;
    }

  7. #7
    Join Date
    Jun 2001
    Location
    Melbourne/Aus (C# .Net 4.0)
    Posts
    686

    Re: dynamic treeview problem

    ahhh, it's a web page, sorry, I assumed it was a Windows Client.

    Code:
    using System;
    using System.Web.UI.WebControls;
    
    public partial class _Default : System.Web.UI.Page 
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // for each item in the database
            {
                TreeNode node = new TreeNode("Death Clock", "http://DeathClock.com");
                TreeView1.Nodes.Add(node);
            }
    
            TreeView1.SelectedNodeChanged += new EventHandler(TreeView1_SelectedNodeChanged);
        }
    
        void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
        {
            TreeView treeView = sender as TreeView;
    
            String theValue = treeView.SelectedNode.Value;
    
            Response.Redirect(theValue);
        }
    }
    Last edited by rliq; September 18th, 2009 at 01:07 AM.
    Rob
    -
    Ohhhhh.... Old McDonald was dyslexic, E O I O EEEEEEEEEE.......

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured