Results 1 to 2 of 2

Thread: Can't get treeview control to stay open asp.net

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2008
    Posts
    513

    Can't get treeview control to stay open asp.net

    Hello: I'm working on a small web site and would like to use the treeview control. This is my first time to use a treeview control.

    I've embedded the treeview in a master page - and When it first loads, it collapses to show only the primary nodes; If I select one of these, it expands as it should to show the child nodes; however, if I click on a child node to display a page, the child nodes collapse after the page opens.

    I'm working with System.Web.UI.WebControls and my tree view is not created dynamically.

    I'm trying to work with some of the events such as TreeView1_TreeNodeExpanded but can't seem to get it to work.

    I would really appreciate any dirrection you can point me in.

    Thanks,
    Proctor

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2008
    Posts
    513

    Fixed

    I was able to find the solution on the internet (http://msdn2.microsoft.com/en-us/mag...c163598.aspx):

    Code:
    Imports System
    Imports System.Web.UI.WebControls
    Imports System.Collections.Generic
    Partial Class SiteNavigation
    Inherits System.Web.UI.MasterPage
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    ' Disable ExpandDepth if the TreeView’s expanded/collapsed 
    ' state is stored in session. 
    If Session("TreeViewState") IsNot Nothing Then
    TreeView1.ExpandDepth = -1
    End If
    End Sub
    Protected Sub TreeView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles TreeView1.DataBound
    If Session("TreeViewState") Is Nothing Then
    ' Record the TreeView’s current expanded/collapsed state. 
    Dim list As New List(Of String)(100)
    SaveTreeViewState(TreeView1.Nodes, list)
    Session("TreeViewState") = list
    Else
    ' Apply the recorded expanded/collapsed state to 
    ' the TreeView. 
    Dim list As List(Of String) = DirectCast(Session("TreeViewState"), List(Of String))
    RestoreTreeViewState(TreeView1.Nodes, list)
    End If
    End Sub
    Protected Sub TreeView1_TreeNodeCollapsed(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.TreeNodeEventArgs) Handles TreeView1.TreeNodeCollapsed
    If IsPostBack Then
    Dim list As New List(Of String)(100)
    SaveTreeViewState(TreeView1.Nodes, list)
    Session("TreeViewState") = list
    End If
    End Sub
     
    Protected Sub TreeView1_TreeNodeExpanded(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.TreeNodeEventArgs) Handles TreeView1.TreeNodeExpanded
    If IsPostBack Then
    Dim list As New List(Of String)(100)
    SaveTreeViewState(TreeView1.Nodes, list)
    Session("TreeViewState") = list
    End If
    End Sub
     
     
    Private Sub SaveTreeViewState(ByVal nodes As TreeNodeCollection, ByVal list As List(Of String))
    ' Recursively record all expanded nodes in the List. 
    For Each node As TreeNode In nodes
    If node.ChildNodes IsNot Nothing AndAlso node.ChildNodes.Count <> 0 Then
    If node.Expanded.HasValue AndAlso node.Expanded.Value = True AndAlso Not [String].IsNullOrEmpty(node.Text) Then
    list.Add(node.Text)
    End If
    'If node.Expanded.HasValue AndAlso node.Expanded = True AndAlso Not [String].IsNullOrEmpty(node.Text) Then
    ' list.Add(node.Text)
    'End If
    SaveTreeViewState(node.ChildNodes, list)
    End If
    Next
    End Sub
    Private Sub RestoreTreeViewState(ByVal nodes As TreeNodeCollection, ByVal list As List(Of String))
    For Each node As TreeNode In nodes
    ' Restore the state of one node. 
    If list.Contains(node.Text) Then
    If node.ChildNodes IsNot Nothing AndAlso node.ChildNodes.Count <> 0 AndAlso node.Expanded.HasValue AndAlso node.Expanded.Value = False Then
    node.Expand()
    End If
    ElseIf node.ChildNodes IsNot Nothing AndAlso node.ChildNodes.Count <> 0 AndAlso node.Expanded.HasValue AndAlso node.Expanded.Value = True Then
    node.Collapse()
    End If
    ' If the node has child nodes, restore their states, too. 
    If node.ChildNodes IsNot Nothing AndAlso node.ChildNodes.Count <> 0 Then
    RestoreTreeViewState(node.ChildNodes, list)
    End If
    Next
    End Sub
     
     
    End Class
    I converted to vb and thought I'd post in case it could be useful to anyone.
    Thanks,
    Proctor

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width