Thursday, September 23, 2010

Retain TreeView Navigation state after postback in asp.net

                                                 I have read one of the article in  in Microsoft site about how  to   Persistent TreeViews navigate among pages .But it is not fit for  my application .The  code is very heavy logic .The application is going to slow (Some time may not respond)because of Maintain the state of tree view control using sessions (Which is burden on Web server ).Every  post back  the tree view control  view-state going to increase.hmmm.... .
               
                                                I'm trying to keep my Master Page as "light" as possible without using a lot of code, because I don't want to slow down the entire site.And Tree-view will be converted to post back every time you click a node .

                                                I'm sure it can be simpler. Another idea was to put Tree View in the i Frame - similar to msdn web site, but that creates some issues with design and SEO.
                   
                                        So i started to refactor the code and architecture as per my project requirement .
               
                   
Here is the advantages of new code .
               
New Improvements


1.    No more round trips between client and server when selection node changed  .
2.    No state maintain in web server.
3.    No more loops for saving and restoring the view state in session
4.    Tree view without view state (Big advantage)
5.    More than six methods/events are removed
6.    Implemented client side redirection instead of server side 


Note: In my application i have created userControl for leftnavigation and in that i have a tree view control

Leftnavigation treeView mark up page

 
 <asp:TreeView ID="TreeviewNavigation" runat="server" NodeWrap="true" EnableViewState="false"

            ExpandDepth="0" ShowLines="true" HoverStyle="border=solid 1px;color=black;background:white;font-size=16;font-weight:bold">

            <SelectedNodeStyle CssClass="SelectedNode"></SelectedNodeStyle>

        </asp:TreeView>





In code behind logic

       protected void PagePreRender(object sender, EventArgs e)

        {

            try

            {

                FillLeftNavigation();

                ExpandTreeView(TreeviewNavigation.Nodes);

            }

            catch (Exception ex)

            {

               // Exception handling logic

            }

        }







       public void FillLeftNavigation()

        {

            

            TreeviewNavigation.ShowExpandCollapse = true;

            // set the default state of all nodes.

            TreeviewNavigation.CollapseAll();

        }



        private void ExpandTreeView(TreeNodeCollection nodes)

        {

            foreach (TreeNode node in nodes)

            {

                //Modifying the url string according to tree view not value format

             string reqNodeValue = HttpContext.Current.Request["ReqVal"];   // Here we are sending treeview selected node value through Query string 

                

                 if (node.Value.ToString() == reqNodeValue)                 //Compare the value with tree view nodes 

                {

                    node.Selected = true;                                   //If value is equal expand it 

                    ExpandParent(node.Parent);

                }



                if (node.ChildNodes.Count > 0)

                {

                    ExpandTreeView(node.ChildNodes);

                }

            }

        }



        private void ExpandParent(TreeNode node)

        {

            if (node != null)

            {

                node.Expand();

                ExpandParent(node.Parent);

            }

        }

5 comments:

Vishnu vardhan said...

Hi,

Wow ! tree view with out view state
can you please give me total code then i can use in my project thank's advance .
Please give me your mail id .

Anonymous said...

Thanks a lot for such an useful post. I am trying this out on a masterpage and the tree is going back to the default view. Have you got it to work on a masterpage? I'd like to know.

Madavan Prabakaran said...

Hi,
I have placed the treeview control in the WebUserControl page. But there is no PagePreRender event in that page., Please explain how to achieve this.

Regards,
Prabakaran.

Uma Sharma said...

Hi,
Can you please send me entire code of treeview. I can't maintain the treeview state on postback.
its really greatful if u send me entire code...
thanks,
Uma Sharma

Kettic said...

Using Kettic TreeView control in .NET application