2

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.

Arioch 'The
  • 15,799
  • 35
  • 62
iMan Biglari
  • 4,674
  • 1
  • 38
  • 83

1 Answers1

1

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.

Aristos
  • 66,005
  • 16
  • 114
  • 150
  • So, the basic idea is that if I disable view state on controls which are not present on both master pages, everything else would work fine? – iMan Biglari Dec 17 '12 at 11:02
  • @iManBiglari Yes this is the idea - in general I disable the viewstate on most of my controls. Only on few I let it. I keep my viewstate very small. – Aristos Dec 17 '12 at 11:03
  • And, I ask this because I'm very new to ASP.Net. Can I disable and enable view state just before switching the master pages? Or it should be always disabled? It's a little hard to explain to my users why the navigation tree doesn't keep track of the page they are visiting, the user name isn't shown, etc. – iMan Biglari Dec 17 '12 at 11:10
  • 1
    @iManBiglari Its by case. You can start by disable the viewstate before the post back. If you understand what the viewstate is you should make your work with out problem. The viewstate is just the previous state of the controls. You keep it only if you won to use it. If not then you do not needed it. You basic only needed on dropdown list, and on GridView controls to remember in witch page you are when you make paginating. And also you may needed in some other case - but you can always do and with out it. – Aristos Dec 17 '12 at 11:48
  • @iManBiglari take a look at this answer http://stackoverflow.com/questions/12086120/how-i-can-deactivate-viewstate-without-control-problems/12086277#12086277 – Aristos Dec 17 '12 at 11:51
  • Can you please assist me with this: http://stackoverflow.com/questions/30673694/how-to-change-sort-order-image-in-a-gridview-sorting-event Thanks., – SearchForKnowledge Jun 05 '15 at 18:58