//
you're reading...
asp.net, javascript Development, VB.Net Development

ASP.Net Treeview Preserve Expansion State for Navigation

Download the code for this post

In reviewing posting related to the issue of preserving an ASP.Net treeview’s expansion state when using it in a master page for navigation purposes I felt that many of the approaches required some updating.

To implement nagivation you need a project with a web.sitemap file, a master page with a treeview, a sitemap datasource, and a class to hold the state between page postings. You can download the code associated with this posting for a complete solution. Here are the highlights.

In the code-behind for the master page.


    '''
<summary> ''' Save the state of all nodes whenever there is a change.
 ''' </summary>
    '''
    ''' This routine is automatically wired-up with the addition of the TreeView.
    ''' It is called without a postback.
    '''
    Private Sub SiteMap_TreeView_TreeNodeChanged(
                    sender As Object,
                    e As System.Web.UI.WebControls.TreeNodeEventArgs) _
                Handles SiteMap_TreeView.TreeNodeCollapsed,
                        SiteMap_TreeView.TreeNodeExpanded

        Dim treeViewState As New Drybridge_TreeViewState()
        treeViewState.TreeView_SaveState(SiteMap_TreeView, Me.GetType.ToString())

    End Sub

    '''
<summary> ''' Prior to rendering the control load the saved expansion status.
 ''' </summary>
    '''
    ''' This will overwrite the system's determination of expandability based on the ExpandDepth setting.
    ''' No need to process on call-backs as the control handles properly.
    '''
    Private Sub SiteMap_TreeView_PreRender(
                    sender As Object,
                    e As System.EventArgs) _
                Handles SiteMap_TreeView.PreRender

        If Not Page.IsCallback Then

            Dim treeViewState As New Drybridge_TreeViewState()
            treeViewState.RestoreTreeView(SiteMap_TreeView, Me.GetType.ToString())
        End If

    End Sub

It is also key for the sitemap datasource to be defined as suppressing the root node.

Advertisement

About art colman

XML tools and consulting for standards development.

Discussion

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.