p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


Go Back   p2p.wrox.com Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Professional
I forgot my password Register Now
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum.

Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Professional section of the Wrox p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.

Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 23rd, 2008, 02:57 AM
Registered User
 
Join Date: Apr 2007
Location: Vejle, Jylland, Denmark.
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Programmatically select a treeview node and expand

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:

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 :-)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Change treeview node font-style programmatically panuvin C# 2005 2 December 15th, 2007 02:59 AM
How to select a node or text from treeview MacDevv C# 2005 3 September 5th, 2006 06:23 AM
asp.net 2.0 treeview select node does not respond vhrao ASP.NET 2.0 Basics 0 February 17th, 2006 05:03 PM
How to get node in treeview ncminhtung BOOK: Professional VB.NET, 2nd Edition or 2003 1 December 7th, 2005 10:06 AM
Add node in ie treeview acko ASP.NET 1.x and 2.0 Application Design 0 January 28th, 2004 09:29 AM



All times are GMT -4. The time now is 01:15 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
© 2008 Wiley Publishing, Inc