Solved

Web control contents not being preserved

Posted on 2004-09-01
3
303 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Suggested Courses

752 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