How to add child node in treeview at runtime in asp.net? - c#

I have treeView control in my web page which has three parent nodes Parent1, Parent2, Parent3. Then how i will add the child at runtime.
Thanks.

You can add parent and child dynamically by creating the objects of TreeNode.
i.e.
foreach (TreeNode objTN in TreeView1.Nodes)
{
if (objTN.Text == "You Parent Name")
{
TreeNode child = new TreeNode();
child.Text = "childText";
objTN.ChildNodes.Add(child);
}
}
Please refer link Adding children to a treenode dynamically in c# for more details.

Related

How to find the index of a root folder in a treeview?

How to find the index of every root folder in a treeview?
Let's say there's a treeview with 4 root nodes. They're all on the same level and all of them have child nodes (not displayed):
|-a
|-b
|-c
|-d
Now let's suppose there's a selected node on the branch of the "c" root node. How could I get the index of the "c" node? (In this case, it's the third one between root nodes).
So, given a selected node, how could I get the index of its root node?
To achieve your task, you should find clicked node's parent and then parent's parent etc... So, we need a recursion here.
Take a look at sample code (with comments):
private void Form1_Load(object sender, EventArgs e)
{
//add test data on form load (you can do it on form design, too.
//there are 4 root nodes and each of them has one subnode.
//Additionally, c's first node, called 'c-1', has it's own child.
treeView1.Nodes.Add(new TreeNode("a"));
treeView1.Nodes.Add(new TreeNode("b"));
treeView1.Nodes.Add(new TreeNode("c"));
treeView1.Nodes.Add(new TreeNode("d"));
treeView1.Nodes[0].Nodes.Add(new TreeNode("a-1"));
treeView1.Nodes[1].Nodes.Add(new TreeNode("b-1"));
treeView1.Nodes[2].Nodes.Add(new TreeNode("c-1"));
treeView1.Nodes[3].Nodes.Add(new TreeNode("d-1"));
treeView1.Nodes[2].Nodes[0].Nodes.Add(new TreeNode("c-1-1"));
//expand those nodes to see things clearly
treeView1.ExpandAll();
//subscribe to after select event. When user selects one node, treeView1_AfterSelect method will be called.
//this can be done on form designer too, on properties panel
treeView1.AfterSelect += treeView1_AfterSelect;
}
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
//this method will be called when you select node
//find topmost parent by calling method FindTopMostParent and passing it selected node
var topmostParent = FindTopMostParent(e.Node);
//here's your index of topmost node (parent or grandparent or grand-grand of selcted node(
var index = treeView1.Nodes.IndexOf(topmostParent);
}
private TreeNode FindTopMostParent(TreeNode node)
{
//first, we check if passed node has parent. If not, return that node, because it's the topmost one
//then, repeat that search for parent again and again until you find that node which does not have parent
while (node.Parent != null)
return FindTopMostParent(node.Parent);
//return parentless node :)
return node;
}

Checkbox checked state of child node

I am using VS 2005 (.NET 2.0) C# working on a Winform with functionality of XML parsing to a TreeView.
I'm planning to enable the checkboxes of the treeview list. Actually, I already enabled them. I found this code here while looking for some properties of TreeView and TreeNode that might be of help. I found TreeNode.Checked Property and TreeView.CheckBoxes Property.
public void HighlightCheckedNodes()
{
int countIndex = 0;
string selectedNode = "Selected customer nodes are : ";
foreach (TreeNode myNode in myTreeView.Nodes[0].Nodes)
{
// Check whether the tree node is checked.
if(myNode.Checked)
{
// Set the node's backColor.
myNode.BackColor = Color.Yellow;
selectedNode += myNode.Text+" ";
countIndex++;
}
else
myNode.BackColor = Color.White;
}
if(countIndex > 0)
MessageBox.Show(selectedNode);
else
MessageBox.Show("No nodes are selected");
}
So I tried it on my test project and it only worked on the parent nodes, not on the child nodes. What I want to do and to happen is that I have to also determine the checked state of the childnodes. I'm working on a project that needs selection of single to multiple child nodes and executing an external program once after checked. Is this possible, with the childnodes? Or are there any other alternatives.
Let me know if I'm unclear on anything. Thank you in advance.
I think you need to change your code to following (It seems to me you have just used code from MSDN). Given code provide a sample answer you could adopt.
// Start from Root type node ; Loop them all
foreach (TreeNode rootNodes in treeView1.Nodes)
{
// Sub note set of current root
foreach (TreeNode childs in rootNodes.Nodes)
{
childs.Text = "Checked"; // Testing for validity : First level of child
/*
* Needs to go further down if we have childs of childs
foreach (TreeNode child_child in childs.Nodes)
{
child_child.Text = "Checked"; // Testing for validity : Second level of child
}
*/
}
}
Explanation :
Given example will change text for following structure (text Checked Applied to childs)
ROOT
|-> Child
|-> Child
ROOT
|-> Child
|-> Child
If you un-comment the inner most foreach you can can apply it to a structure like below ((text Checked Applied to childs))
ROOT
|-> Child
|-> Child
|-> Child
|-> Child
ROOT
|-> Child
|-> Child
In your code you don't progress further down a TreeNode and you have forgotten that each TreeNode could contains it's own child node collection.
Simply said,
myTreeView.Nodes[0].Nodes // <- This is wrong for what you want

TreeView added to object not showing up in UI

I am trying to create a dialog where the user selects a node in a TreeView so that he can save some data to that node.
The tree itself is created in a separate control (that is used for browsing/opening content of nodes) and my idea is that i should use that tree in my save control.
My constructor looks like this:
public FrmSaveToRepository(TreeView tree)
{
InitializeComponent();
this.treeView = tree;
}
but when the form holding the control pops up the treeView is empty. Did i miss a step?
I open my form with this code:
var frm = new FrmSaveToRepository(tree);
frm.Show();
Right so the correct way to add the old nodes to the new tree is:
public FrmSaveToRepository(TreeView tree)
{
InitializeComponent();
foreach (TreeNode node in tree.Nodes)
{
this.treeView1.Nodes.Add(node.Clone() as TreeNode);
}
}
but that still means that all the events and handlers have to be implemented separately in the new tree.

ASP.NET TreeView + AJAX [duplicate]

This question already has answers here:
ASP.NET TreeView and loading data on demand
(2 answers)
Closed 6 years ago.
I have a treeview I'm using to display a directory structure. I am trying to reduce load times by loading sub nodes on node expansion. Is there a way to do this?
Below is the code I'm currently using to populate the treeview:
protected void Page_Load(object sender, EventArgs e) {
BuildTree(Request.QueryString["path"]);
}
private void BuildTree(string dirPath)
{
//get root directory
System.IO.DirectoryInfo rootDir = new System.IO.DirectoryInfo(dirPath);
//create and add the root node to the tree view
TreeNode rootNode = new TreeNode(rootDir.Name, rootDir.FullName);
TreeView1.Nodes.Add(rootNode);
//begin recursively traversing the directory structure
TraverseTree(rootDir, rootNode);
}
private void TraverseTree(System.IO.DirectoryInfo currentDir, TreeNode currentNode)
{
//loop through each sub-directory in the current one
foreach (System.IO.DirectoryInfo dir in currentDir.GetDirectories())
{
//create node and add to the tree view
TreeNode node = new TreeNode(dir.Name, dir.FullName);
currentNode.ChildNodes.Add(node);
//recursively call same method to go down the next level of the tree
TraverseTree(dir, node);
}
foreach (System.IO.FileInfo file in currentDir.GetFiles())
{
TreeNode node = new TreeNode(file.Name, file.FullName);
currentNode.ChildNodes.Add(node);
}
}
For loading the nodes on demand, meaning that the children of a node will be loaded only when the parent node is expanded. Do the following steps:
1 - Set the TreeView.ExpandDepth to 0. This eliminates the expansion of the added TreeNode objects in the TreeView and shows the expansion symbol [+] next to each TreeNode that has the TreeNode.PopulateOnDemand property set to true.
2- Set the TreeNode.PopulateOnDemand to True for each branch TreeNode. When the TreeNode.ChildNodes collection is empty, the expansion symbol [+] will be showed only next to TreeNode objects that has TreeNode.PopulateOnDemand property set to true.
3- Handle the TreeView.TreeNodePopulate event to poulate branch nodes on expansion. This event will be fired when a TreeNode - with the TreeNode.PopulateOnDemand set to true - has been expanded right before the TreeView.TreeNodeExpanded event gets fired.
Source: ASP.NET TreeView and loading data on demand

How to add editable first child node in winforms

I have a treeview which represents filesystem, I have a New folder button, It works fine if the selected node has some child nodes, however when selected node does not have any child nodes it is not able to add a editable child node. I am using the following code:
tree.LabelEdit = true;
TreeNode node = new TreeNode("New Folder");
if(tree.SelectedNode.Nodes.Count>0)
tree.SelectedNode.Expand();
tree.SelectedNode.Nodes.Add(node);
if(tree.SelectedNode.Nodes.Count>0)
tree.SelectedNode.Expand();
node.BeginEdit();
Any ideas on whats going wrong?
Just change two lines in your code like that:
public Form1()
{
InitializeComponent();
tree.SelectedNode = tree.Nodes.Add("Hello", "Hello");
}
private void button1_Click(object sender, EventArgs e)
{
tree.LabelEdit = true;
TreeNode node = new TreeNode("New Folder");
tree.SelectedNode.Nodes.Add(node);
tree.SelectedNode.Expand();
node.BeginEdit();
}
Explanation: You cannot expand a node which hasn't child nodes. So you have to add the node before you expand the parent node.

Categories

Resources