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
Re: dynamic treeview problem
Is there no 1 who knows the answer to this?/?
Even how to go about information will do.
Re: dynamic treeview problem
Quote:
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.
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).
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;
}
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);
}
}