Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Programmatically select a treeview node and expand the depth

Programmatically select a treeview node and expand the depth

Scheduled Pinned Locked Moved ASP.NET
helpdatabasedesignsysadmin
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    simsen
    wrote on last edited by
    #1

    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:

    <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++)
    
    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups