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);

            }

        }