Question

I have created two master pages for my website. one for normal use, and another for print. On my normal master page, I have a button which sets Session['P'] to '1'. On the print master page, I have another button which sets Session['P'] to '0'. And in my Global.asax.pas, I have the following code to determine which master page to use:

method Global.page_PreInit(sender: System.Object; e: EventArgs);
begin
  var p: System.Web.UI.Page := System.Web.UI.Page(self.Context.Handler);
  if p <> nil then   
    if Session['P'].ToString = '1' then
      p.MasterPageFile := '~/Print.Master'
    else
      p.MasterPageFile := '~/Site.Master'; 
end;

After settings Session['P'], I need to reload the page for its master to change. I need the view state of all my controls to be preserved, and thus can't use Response.Redirect(). I tried using Server.Transfer(Request.Url.AbsolutePath, True);, but it raises the following exception. How can I work around it?

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. For example, when adding controls dynamically, the controls added during a post-back must match the type and position of the controls added during the initial request.

Était-ce utile?

La solution

I am afraid that there is no work around - you change the controls tree of the viewstate after the post back.

You can only disable the viewstate on the controls on the master pages, the controls that conflict with the two master pages.

The general idea is that you disable the viewstate on the controls that have the problem, or you try to use the same control on the two master pages, with the same ids.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top