I have a treeview I use as a menu. The treeview works perfect. BUT now I want to make a function where I add a new node to the menu programmatically. (I don't need help to add the new node in the database) I need help for: reload the menu and select the
newly added node and expand the treeview to that node. I tried a lot, but nothing works for me. I hope someone here can help me with my problem:
//In this button I want to create the node (I don't need help for that) AND then
//Reload the menu and select the newly added node (by expanding the treeview)
//string top = MenuTree.SelectedNode.ValuePath;
//ExpandNodes("s19//24");
//16-06-2008 AS
//Get the top menu
MenuTree.Nodes.Clear();
try
{
DALMenu menu =
new DALMenu();if (menu.EditGetTopMenu(MenuTree) ==
Status.Success)
{
foreach (TreeNode node
in MenuTree.Nodes)
{
if (node.Text ==
"Contact us")
{
node.Selected = true;
}
}
}
}
catch
{ }
}
It reload the menu, but don't get into my child node (which I have choose to be Contact Us).
My tree look like this:
Frontpage
Products
...Product1
...Product2
About us
...Our company
...Contact us
I cann't figure out why It doesn't find the Contact us?
I have also tried to make a find node..... It cann't find the node. When I hold the mouse over the "Contact us" node, the valuePath is: s19\\24. Why is it \\ when I in my code have made the PathSeparator as / (PathSeparator="/")
?
From the code that you provided we can see, you only retrieval the first level of the TreeNodes, but the contact use is in the second level, viz it is About us's chlildNode.
You can try to edit the code like below:
DALMenu menu = new DALMenu();
if (menu.EditGetTopMenu(MenuTree) == Status.Success)
{ for(int i=0;i<MenuTree.Nodes.Count;i++)
{ foreach (TreeNode node in MenuTree.Nodes[i].ChildNodes)
{
if (node.Text == "Contact us")
{
node.Selected = true;
}
}
}
}
Hope it helps.
Amanda Wang
Microsoft Online Community Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
MenuTree.Nodes.Clear();
try
{
DALMenu menu = new DALMenu();
if (menu.EditGetTopMenu(MenuTree) == Status.Success)
{
for (int i = 0; i < MenuTree.Nodes.Count; i++)
{
foreach (TreeNode node in MenuTree.Nodes)
{
if (node.Text == "Contact us")
{
node.Selected = true;
}
}
}
}
}
catch
{ }
I think you are right about, I don't get the childnodes populated. I populate the childnodes when clicking on a specific node. I then tried the following also with no success (it populated the child nodes as many times as there were childnodes but didn't expanded and selected the "Contact us" node):
MenuTree.Nodes.Clear();
try
{
DALMenu menu = new DALMenu();
if (menu.EditGetTopMenu(MenuTree) == Status.Success)
{
for (int i = 0; i < MenuTree.Nodes.Count; i++)
{
foreach (TreeNode node in MenuTree.Nodes)
{
try
{
DALMenu submenu = new DALMenu();
submenu.EditGetSubMenu(node);
}
catch
{ }
if (node.Text == "Kontakt os")
{
node.Selected = true;
}
}
}
}
}
catch
{ }
MenuTree.Nodes.Clear();
try
{
DALMenu menu =
new DALMenu();if (menu.EditGetTopMenu(MenuTree) ==
Status.Success)
{
for (int i = 0; i < MenuTree.Nodes.Count; i++)
{
foreach (TreeNode node
in MenuTree.Nodes)
{
try
{
DALMenu submenu =
new DALMenu();
submenu.EditGetSubMenu(node);
}
catch
{ }
if (node.Text ==
"Kontakt os")
{
node.Selected = true;
}
}
}
}
}
catch
{ }
I hope you can help me with telling me where and how to get the childnodes :-)
//In this button I want to create the node (I don't need help for that) AND then
//Reload the menu and select the newly added node (by expanding the treeview)
//string top = MenuTree.SelectedNode.ValuePath;
//ExpandNodes("s19//24");
//16-06-2008 AS
//Get the top menu
MenuTree.Nodes.Clear();
try
{
DALMenu menu =
new DALMenu();if (menu.EditGetTopMenu(MenuTree) ==
Status.Success)
{
for (int i = 0; i < MenuTree.Nodes.Count; i++)
{
foreach (TreeNode node
in MenuTree.Nodes[i].ChildNodes)
{
if (node.Text ==
"Contact us")
{
node.Selected = true;
}
}
}
}
}
catch
{ }
}
It only goes to the first level it - doesn't expand anything. Hope you are still on with this. In advanced thank you for your help :-)
protected void Button1_Click(object sender, EventArgs e)
{
//In this button I want to create the node (I don't need help for that) AND then
//Reload the menu and select the newly added node (by expanding the treeview)
//string top = MenuTree.SelectedNode.ValuePath;
//ExpandNodes("s19//24");
//16-06-2008 AS
//Get the top menu
MenuTree.Nodes.Clear();
try
{
DALMenu menu = new DALMenu();
if (menu.EditGetTopMenu(MenuTree) == Status.Success)
{
for (int i = 0; i < MenuTree.Nodes.Count; i++)
{
//foreach (TreeNode node in MenuTree.Nodes[i].ChildNodes)
//{
//if (node.Text == "Contact us")
//{
//node.Selected = true;
//}
//} retrieveNodes(MenuTree.Nodes[i]); }
}
}
catch
{ }
}
2. the code to search the contact us node:
protected void Button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < this.TreeView1.Nodes.Count; i++)
{
retrieveNodes(TreeView1.Nodes[i]);
}
}
simsen
Member
229 Points
235 Posts
Programmatically select a treeview node and expand the depth
Jun 20, 2008 08:28 AM|LINK
Hi,
I have a treeview I use as a menu. The treeview works perfect. BUT now I want to make a function where I add a new node to the menu programmatically. (I don't need help to add the new node in the database) I need help for: reload the menu and select the newly added node and expand the treeview to that node. I tried a lot, but nothing works for me. I hope someone here can help me with my problem:
My code:
TestMenu.aspx:
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"><
asp:TreeView ID="MenuTree" EnableClientScript="False" runat="server" ExpandDepth="15" ShowExpandCollapse="False" ontreenodeexpanded="MenuTree_TreeNodeExpanded" ontreenodepopulate="MenuTree_TreeNodePopulate" PathSeparator="/" ontreenodecollapsed="MenuTree_TreeNodeCollapsed"> <NodeStyle CssClass="menulink" /> <SelectedNodeStyle CssClass="menulink" /> </asp:TreeView>
<br /><br /><br />ID:
<asp:Label ID="lblMenuId" runat="server" Text="Label"></asp:Label> <br /><br /> <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /></
asp:Content>TestMenu.aspx.cs:
public partial class TestMenu : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { //16-06-2008 AS //Get the top menu try { DALMenu menu = new DALMenu(); if (menu.EditGetTopMenu(MenuTree) == Status.Success) { } } catch { } } } protected void MenuTree_TreeNodePopulate(object sender, TreeNodeEventArgs e) { if (e.Node.Depth == 0) { try { DALMenu submenu = new DALMenu(); submenu.EditGetSubMenu(e.Node); } catch { } } } protected void MenuTree_TreeNodeExpanded(object sender, TreeNodeEventArgs e) { try { int key_id = Convert.ToInt32(e.Node.Value); lblMenuId.Text = key_id.ToString(); if (e.Node.Depth == 0) { TreeView t = (TreeView)sender; for (int i = 0; i < t.Nodes.Count; i++) { if (t.Nodes[i] != e.Node) { t.Nodes[i].CollapseAll(); } } } } catch { } } protected void MenuTree_TreeNodeCollapsed(object sender, TreeNodeEventArgs e) { try { int key_id = Convert.ToInt32(e.Node.Value); lblMenuId.Text = key_id.ToString(); } catch { } } private void ExpandNodes(string valuepath) { string[] tmp = valuepath.Split('/'); string tmpValuePath = string.Empty; for (int i = 0; i < tmp.Length; i++) { if (i == 0) tmpValuePath = tmp[i]; else tmpValuePath += "/" + tmp[i]; TreeNodeEventArgs e = new TreeNodeEventArgs(MenuTree.FindNode(tmpValuePath)); e.Node.ChildNodes.Clear(); e.Node.PopulateOnDemand = false; MenuTree_TreeNodePopulate(MenuTree, e); MenuTree.FindNode(tmpValuePath).Expand(); MenuTree.FindNode(tmpValuePath).Selected = true; } } protected void Button1_Click(object sender, EventArgs e) { //In this button I want to create the node (I don't need help for that) AND then //Reload the menu and select the newly added node (by expanding the treeview) //string top = MenuTree.SelectedNode.ValuePath; ExpandNodes("s19//24"); } }Kind regards,simsen :-)
Amanda Wang ...
All-Star
30008 Points
3104 Posts
Re: Programmatically select a treeview node and expand the depth
Jun 23, 2008 06:18 AM|LINK
Hi,
After you add the new Node successfully, then call your EditGetTopMenu method to reload the menu.
And you can set the new added node's selected property is true to make the node selected.
We see there is a method ExpandNodes, you can try to pass the new node's valuePath to the ExpandNodes method to expand the new node.
Hope it helps.
Microsoft Online Community Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
simsen
Member
229 Points
235 Posts
Re: Programmatically select a treeview node and expand the depth
Jun 23, 2008 08:51 AM|LINK
Hi Amanda,
Thanks for your reply.
I tried that but it didn't work.
I have this code in my (new added node) button:
protected void Button1_Click(object sender, EventArgs e){
//In this button I want to create the node (I don't need help for that) AND then //Reload the menu and select the newly added node (by expanding the treeview) //string top = MenuTree.SelectedNode.ValuePath; //ExpandNodes("s19//24"); //16-06-2008 AS //Get the top menuMenuTree.Nodes.Clear();
try{
DALMenu menu = new DALMenu();if (menu.EditGetTopMenu(MenuTree) == Status.Success){
foreach (TreeNode node in MenuTree.Nodes){
if (node.Text == "Contact us"){
node.Selected = true;}
}
}
}
catch{ }
}
It reload the menu, but don't get into my child node (which I have choose to be Contact Us).My tree look like this:
Frontpage
Products
...Product1
...Product2
About us
...Our company
...Contact us
I cann't figure out why It doesn't find the Contact us?
I have also tried to make a find node..... It cann't find the node. When I hold the mouse over the "Contact us" node, the valuePath is: s19\\24. Why is it \\ when I in my code have made the PathSeparator as / (PathSeparator="/") ?
I hope you can help me. I'm pretty desperate
Kind regards,
simsen :-)
Amanda Wang ...
All-Star
30008 Points
3104 Posts
Re: Programmatically select a treeview node and expand the depth
Jun 24, 2008 02:52 AM|LINK
Hi,
From the code that you provided we can see, you only retrieval the first level of the TreeNodes, but the contact use is in the second level, viz it is About us's chlildNode.
You can try to edit the code like below:
DALMenu menu = new DALMenu();
if (menu.EditGetTopMenu(MenuTree) == Status.Success)
{
for(int i=0;i<MenuTree.Nodes.Count;i++)
{
foreach (TreeNode node in MenuTree.Nodes[i].ChildNodes)
{
if (node.Text == "Contact us")
{
node.Selected = true;
}
}
}
}
Hope it helps.
Microsoft Online Community Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
simsen
Member
229 Points
235 Posts
Re: Programmatically select a treeview node and expand the depth
Jun 24, 2008 06:34 AM|LINK
Hi Amanda,
I tried the following with no success:
MenuTree.Nodes.Clear(); try { DALMenu menu = new DALMenu(); if (menu.EditGetTopMenu(MenuTree) == Status.Success) { for (int i = 0; i < MenuTree.Nodes.Count; i++) { foreach (TreeNode node in MenuTree.Nodes) { if (node.Text == "Contact us") { node.Selected = true; } } } } } catch { }I think you are right about, I don't get the childnodes populated. I populate the childnodes when clicking on a specific node. I then tried the following also with no success (it populated the child nodes as many times as there were childnodes but didn't expanded and selected the "Contact us" node):
MenuTree.Nodes.Clear(); try { DALMenu menu = new DALMenu(); if (menu.EditGetTopMenu(MenuTree) == Status.Success) { for (int i = 0; i < MenuTree.Nodes.Count; i++) { foreach (TreeNode node in MenuTree.Nodes) { try { DALMenu submenu = new DALMenu(); submenu.EditGetSubMenu(node); } catch { } if (node.Text == "Kontakt os") { node.Selected = true; } } } } } catch { }MenuTree.Nodes.Clear();
try{
DALMenu menu = new DALMenu();if (menu.EditGetTopMenu(MenuTree) == Status.Success){
for (int i = 0; i < MenuTree.Nodes.Count; i++){
foreach (TreeNode node in MenuTree.Nodes){
try{
DALMenu submenu = new DALMenu();submenu.EditGetSubMenu(node);
}
catch{ }
if (node.Text == "Kontakt os"){
node.Selected = true;}
}
}
}
}
catch{ }
I hope you can help me with telling me where and how to get the childnodes :-)
Kind regards,
simsen :-)
Amanda Wang ...
All-Star
30008 Points
3104 Posts
Re: Programmatically select a treeview node and expand the depth
Jun 24, 2008 08:25 AM|LINK
Hi,
You need to edit the code like below:
for (int i = 0; i < MenuTree.Nodes.Count; i++)
{
foreach (TreeNode node in MenuTree.Nodes[i].ChildNodes)
{
if (node.Text == "Contact us")
{
node.Selected = true;
}
}
}
Microsoft Online Community Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
simsen
Member
229 Points
235 Posts
Re: Programmatically select a treeview node and expand the depth
Jun 27, 2008 06:23 AM|LINK
Hi Amanda,
Sorry I haven't answered you, but I have been seek the last few days.
I tried the following with no success
protected void Button1_Click(object sender, EventArgs e){
//In this button I want to create the node (I don't need help for that) AND then //Reload the menu and select the newly added node (by expanding the treeview) //string top = MenuTree.SelectedNode.ValuePath; //ExpandNodes("s19//24"); //16-06-2008 AS //Get the top menuMenuTree.Nodes.Clear();
try{
DALMenu menu = new DALMenu();if (menu.EditGetTopMenu(MenuTree) == Status.Success){
for (int i = 0; i < MenuTree.Nodes.Count; i++){
foreach (TreeNode node in MenuTree.Nodes[i].ChildNodes){
if (node.Text == "Contact us"){
node.Selected = true;}
}
}
}
}
catch{ }
}
It only goes to the first level it - doesn't expand anything. Hope you are still on with this. In advanced thank you for your help :-)
Kind regards,
simsen :-)
Amanda Wang ...
All-Star
30008 Points
3104 Posts
Re: Programmatically select a treeview node and expand the depth
Jun 27, 2008 06:40 AM|LINK
Hi,
Try to use recursion retrieve the treenode:
protected void Button1_Click(object sender, EventArgs e)
{
//In this button I want to create the node (I don't need help for that) AND then
//Reload the menu and select the newly added node (by expanding the treeview)
//string top = MenuTree.SelectedNode.ValuePath;
//ExpandNodes("s19//24");
//16-06-2008 AS
//Get the top menu
MenuTree.Nodes.Clear();
try
{
DALMenu menu = new DALMenu();
if (menu.EditGetTopMenu(MenuTree) == Status.Success)
{
for (int i = 0; i < MenuTree.Nodes.Count; i++)
{
//foreach (TreeNode node in MenuTree.Nodes[i].ChildNodes)
//{
//if (node.Text == "Contact us")
//{
//node.Selected = true;
//}
//}
retrieveNodes(MenuTree.Nodes[i]);
}
}
}
catch
{ }
}
protected void retrieveNodes(TreeNode node)
{
if(node.Text == "Contact us")
{
node.Selected = true;
}
else
{
if(node.ChildNode.Count != 0)
{
foreach(TreeNode tn in node.ChildNodes)
{
if(tn.Text =="Contact us")
{
tn.Selected = true;
}
else
{
if(tn.ChildNode.Count != 0)
{
retrieveNodes(tn);
}
}
}
}
}
}
Hope it helps.
Microsoft Online Community Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
simsen
Member
229 Points
235 Posts
Re: Programmatically select a treeview node and expand the depth
Jun 27, 2008 07:51 AM|LINK
I'm sorry, but that didn't help either.
I post the entirely code, so you can see, what I have now:
aspx:
<asp:TreeView ID="MenuTree" EnableClientScript="False" runat="server" ExpandDepth="15" ShowExpandCollapse="False" ontreenodeexpanded="MenuTree_TreeNodeExpanded" ontreenodepopulate="MenuTree_TreeNodePopulate" PathSeparator="/" ontreenodecollapsed="MenuTree_TreeNodeCollapsed"> <NodeStyle CssClass="menulink" /> <SelectedNodeStyle CssClass="menulink" /> </asp:TreeView>
<br /><br /><br />ID:
<asp:Label ID="lblMenuId" runat="server" Text="Label"></asp:Label> <br /><br /> <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />aspx.cs:
using System; using System.Collections; using System.Configuration; using System.Data; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; public partial class TestMenu : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { //16-06-2008 AS //Get the top menu try { DALMenu menu = new DALMenu(); if (menu.EditGetTopMenu(MenuTree) == Status.Success) { } } catch { } } } protected void MenuTree_TreeNodePopulate(object sender, TreeNodeEventArgs e) { if (e.Node.Depth == 0) { try { DALMenu submenu = new DALMenu(); submenu.EditGetSubMenu(e.Node); } catch { } } if (e.Node.Depth == 1) { try { DALMenu submenu = new DALMenu(); submenu.EditGetSubMenu(e.Node); } catch { } } if (e.Node.Depth == 2) { try { DALMenu submenu = new DALMenu(); submenu.EditGetSubMenu(e.Node); } catch { } } } protected void MenuTree_TreeNodeExpanded(object sender, TreeNodeEventArgs e) { try { int key_id = Convert.ToInt32(e.Node.Value); lblMenuId.Text = key_id.ToString(); if (e.Node.Depth == 0) { TreeView t = (TreeView)sender; for (int i = 0; i < t.Nodes.Count; i++) { if (t.Nodes[i] != e.Node) { t.Nodes[i].CollapseAll(); } } } } catch { } } protected void MenuTree_TreeNodeCollapsed(object sender, TreeNodeEventArgs e) { try { int key_id = Convert.ToInt32(e.Node.Value); lblMenuId.Text = key_id.ToString(); } catch { } } private void ExpandNodes(string valuepath) { string[] tmp = valuepath.Split('/'); string tmpValuePath = string.Empty; for (int i = 0; i < tmp.Length; i++) { if (i == 0) tmpValuePath = tmp[i]; else tmpValuePath += "/" + tmp[i]; TreeNodeEventArgs e = new TreeNodeEventArgs(MenuTree.FindNode(tmpValuePath)); e.Node.ChildNodes.Clear(); e.Node.PopulateOnDemand = false; MenuTree_TreeNodePopulate(MenuTree, e); MenuTree.FindNode(tmpValuePath).Expand(); MenuTree.FindNode(tmpValuePath).Selected = true; } } protected void Button1_Click(object sender, EventArgs e) { //In this button I want to create the node (I don't need help for that) AND then //Reload the menu and select the newly added node (by expanding the treeview) //string top = MenuTree.SelectedNode.ValuePath; //ExpandNodes("s19//24"); //16-06-2008 AS //Get the top menu MenuTree.Nodes.Clear(); try { DALMenu menu = new DALMenu(); if (menu.EditGetTopMenu(MenuTree) == Status.Success) { for (int i = 0; i < MenuTree.Nodes.Count; i++) { //foreach (TreeNode node in MenuTree.Nodes[i].ChildNodes) //{ // if (node.Text == "Contact us") // { // node.Selected = true; // } //} retrieveNodes(MenuTree.Nodes[i]); } } } catch { } } protected void retrieveNodes(TreeNode node) { if (node.Text == "Contact us") { node.Selected = true; } else { if (node.ChildNodes.Count != 0) { foreach (TreeNode tn in node.ChildNodes) { if (tn.Text == "Contact us") { tn.Selected = true; } else { if (tn.ChildNodes.Count != 0) { retrieveNodes(tn); } } } } } } }Kind regards,
simsen :-)
Amanda Wang ...
All-Star
30008 Points
3104 Posts
Re: Programmatically select a treeview node and expand the depth
Jun 27, 2008 08:52 AM|LINK
Hi,
Sorry, we can't use your to repro your problem, we have a test use the above recursion method to search TreeNode, it works fine:
Below is our test code:
1. SiteMap:
<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
<siteMapNode url="" title="Home" description="">
<siteMapNode url="~/Default.aspx" title="Page1" description="" />
<siteMapNode url="~/Default2.aspx" title="Page2" description="" >
<siteMapNode url="Default3.aspx" title="Page3" description="">
<siteMapNode url="" title="contact us"></siteMapNode>
</siteMapNode>
</siteMapNode>
</siteMapNode>
</siteMap>
2. the code to search the contact us node:
protected void Button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < this.TreeView1.Nodes.Count; i++)
{
retrieveNodes(TreeView1.Nodes[i]);
}
}
protected void retrieveNodes(TreeNode node)
{
if (node.Text.ToLower() == "contact us")
{
node.Selected = true;
}
else
{
if (node.ChildNodes.Count != 0)
{
foreach (TreeNode tn in node.ChildNodes)
{
if (tn.Text.ToLower() == "contact us")
{
tn.Selected = true;
}
else
{
if (tn.ChildNodes.Count != 0)
{
retrieveNodes(tn);
}
}
}
}
}
}
So we would suggest that you can make sure the contact us node is exists, then you can try to have a debug step by step to check the code.
Hope it helps.
Microsoft Online Community Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help.