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.
Said with other words: I need a way to expand the treeview and make a specific node selected
I hope someone here can help me with my problem 
My code:
TestMenu.aspx:
ExpandDepth="15" ShowExpandCollapse="False"
ontreenodeexpanded="MenuTree_TreeNodeExpanded"
ontreenodepopulate="MenuTree_TreeNodePopulate"
PathSeparator="/" ontreenodecollapsed="MenuTree_TreeNodeCollapsed">
ID:
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 
PANKAJ KUMARPosted Oct 30, 2014, 2:00 AM
Thank you very much...it works to expand the perticular node in treeview. it solved my problem.
Scott LyslePosted Jun 28, 2008, 7:55 PM
You'd think that it would work; what I have found is that you have accumulate the value path as you go. For example if you had a node called Flowers and under the Flowers node, you had a node called Daisy, and you wanted to open the treeview up to expand daisy, you'd (hardcoding it) would end up doing something like this:
TreeView1.CollapseAll()
TreeView1.FindNode(
"Flowers").Select()TreeView1.FindNode(
"Flowers").Expand()TreeView1.FindNode(
"Flowers/Daisy").Select()TreeView1.FindNode(
"Flowers/Daisy").Expand()If you tried to just expand Flowers/Daisy from a collapsed treeview, it does not do it.