Restoring VIEWSTATE for dynamicly changing PageTemplates

  • Thread starter Thread starter muesliflakes
  • Start date Start date
M

muesliflakes

I am building a PageTemplate system that features Dynamic Templates,
this class extends System.Web.UI.Page.

A template is selected based on user preference and the Page Controls
are created both before and after the controls from the content page.

I have built a preferences page that alows the user to change the
layout
of the page but and this is throwing an exception

System.Web.HttpException: Failed to load viewstate. The control tree
into which viewstate is being loaded must match the control tree that
was used to save viewstate during the previous request.

I don't really want to turn of state tracking for the components that
are part of the layout areas of the page so I was wondering how I
could handle this state management issue.

Cheers David... I have included the calling method that builds up the
page

protected override void OnInit(EventArgs e)
{
// Get a TEMPLATE by looking up a user preference.
ITemplateLayout layout = TemplateLayout;

// Call Before content methods
layout.PageStart ( this, Controls );
layout.InsertHead ( this, Controls );
layout.PageMiddle ( this, Controls );

layout.BeforeForm ( this, Controls );
layout.BeforeContent ( this, MainForm.Controls );

// Load Content controls from content page
for(int i = 0; i < ContentControls.Count; i++)
{
MainForm.Controls.Add( ContentControls );
}

// Call after content methods
layout.AfterContent ( this, MainForm.Controls );
Controls.Add ( MainForm );
layout.AfterForm ( this, Controls );
layout.PageEnd ( this, Controls );

base.OnInit(e);
}
 
Still hoping someone can help out with this problem.

I've tried a bit of a hack to solve the issue, where by I point any
layout code in to PlaceHolders called Before Content and After Content
and for these place holders I disable viewstate completly.

I don't really want to do this, but because of the dynamic nature of
the layouts, I cant tell whether a menu is going to be on the left or
the right of the page, or the location of any other tile for that
mater.

I've tried the following code but still get a
System.Web.HttpException: Failed to load viewstate when changing from
one layout style to another.

protected override void OnInit(EventArgs e)
{
ITemplateLayout layout = TemplateLayout;

BeforeContent.EnableViewState = false;
AfterContent.EnableViewState = false;

// Start of page, Head section, Start of Body
layout.PageStart ( this, BeforeContent.Controls );
layout.InsertHead ( this, BeforeContent.Controls );
layout.PageMiddle ( this, BeforeContent.Controls );

// Any layout code that is before your page content can go into
either the BeforeForm or BeforeContent methods
layout.BeforeForm ( this, BeforeContent.Controls ); // Before
the starting FORM tag

layout.BeforeContent ( this, MainForm.Controls ); // After
the starting FORM tag

// Load Content controls from Page
for(int i = 0; i < ContentControls.Count; i++)
{
MainForm.Controls.Add( ContentControls );
}

// Any layout code that is after your page content can go into
either the AfterForm or AfterContent methods
layout.AfterContent ( this, MainForm.Controls ); // Before the
closing FORM tag
layout.AfterForm ( this, AfterContent.Controls ); //
Affter the closing FORM tag

layout.PageEnd ( this, AfterContent.Controls );

Controls.Add( BeforeContent );
Controls.Add( MainForm );
Controls.Add( AfterContent );

base.OnInit(e);
}
 
Back
Top