Solved

Web control contents not being preserved

Posted on 2004-09-01
3
298 Views
Last Modified: 2012-06-21
Maybe someone can help me with this problem:

I have a treeview in my detail.aspx (which runs in a frame) like so:

<wtv:TreeView id="docTree"             
      runat="server" NodeCssClass="MyNode"
      NodeImageUrlExpanded="images/FolderExpanded.gif"
      NodeImageUrl="images/Folder.gif"
      TemplateDataBinding="false" Animate="false"
      AutoPostBack="false"></wtv:TreeView>

Most of that is irrelevant but I want to be complete. In my .cs file, it is:

      public class detail : System.Web.UI.Page
      {
            protected PowerUp.Web.UI.WebTree.TreeView docTree;
            .....
      }

In Page_Load, I build the tree and add nodes. I have logic so I only do this once, I don't want to rebuild the tree every time. When this page is reloaded, which is always by a javascript call in another page:

      var theFrame = self.frames["detail"];    
      theFrame.location = "detail.aspx";

but although the treeview displays fine the first time, on every subsequent load of the page, the treeview.Nodes.Count==0  !! I know I am missing something important here, but I can't imagine what. Once I build the tree, the nodes should be preserved, it is a server control.
0
Comment
Question by:RichardFox
  • 2
3 Comments
 
LVL 8

Accepted Solution

by:
shovavnik earned 500 total points
ID: 11954189
Server controls do not automatically preserve their values.  They need to be told to do so.

In addition, they usually preserve their values only on PostBacks, which are essentially "submits" to the same page.  Changing the url of the page does not cause a postback event.  You'll notice that the value of IsPostBack is always false in your code.

Furthermore, when server controls *do* preserve their values, it's because they *are* rebuilt.  By saying "preserve", the meaning is that they cache their data and then load it using a faster or more efficient method.

I think what you want to do is always rebuild the tree, but also always try to load the data from some cached location.  Sample code for getting the data:

... Page_Load() {
XmlDocument treeDoc = null;
// first try to get it from the session
object treeInSession = Session[ "TREE_DOCUMENT" ];
// check if the session is null - this indicates that you need to load the data from the database, or wherever
if( treeInSession == null ) {
  treeDoc = GetTreeXmlFromDataBase();
  // save it to the session so subsequent calls load it without the database
  Session[ "TREE_DOCUMENT" ] = treeDoc;
}
// if the session exists, then use that data
else {
  treeDoc = (XmlDocument) treeInSession;
}
// now bind your tree
MyTree.DataSource = treeDoc; // or whatever the name of the property is
}
0
 

Author Comment

by:RichardFox
ID: 11956219
That's exactly what I needed to know! You were abs. right, that IsPostBack is always false. THanks for the education.
0
 
LVL 8

Expert Comment

by:shovavnik
ID: 11956752
You're welcome.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now