0

I'm using this code in my Global.asax to change master page:

method Global.Application_PreRequestHandlerExecute(src: System.Object; e: EventArgs);
begin
  var p: System.Web.UI.Page := System.Web.UI.Page(self.Context.Handler);
  if p <> nil then
    p.PreInit += new EventHandler(page_PreInit)
end;

method Global.page_PreInit(sender: System.Object; e: EventArgs);
var
  S: String;
begin
  var p: System.Web.UI.Page := System.Web.UI.Page(self.Context.Handler);
  if p <> nil then
    if p.Master <> nil then
    begin
      if Request.Params['__EVENTTARGET'] <> nil then
      begin
        S := Request.Params['__EVENTTARGET'];
        if S.Length > 0 then
          S := S.Substring(S.IndexOf('$') + 1);
        if S = 'lbPrint' then
          Session['P'] := '1'
        else if S = 'lbNormal' then
          Session['P'] := '0';

        if Session['P'].ToString = '1' then
          S := '/Print.Master'
        else
          S := '/Site.Master';
        if not p.MasterPageFile.ToUpper.Equals(S.ToUpper) then
          p.MasterPageFile := S;
      end;
    end;
end;

Whenever the master page is changed, viewstate of all controls in the content page is lost. I'd like to know how to preserve them...

Ken White
  • 123,280
  • 14
  • 225
  • 444
iMan Biglari
  • 4,674
  • 1
  • 38
  • 83

1 Answers1

1

Changing the masterpage does affect the resulting control structure. Therefore ASP.NET is not able to load the viewstate, as it uses control-indicies to do so.

I don't know which version of .NET you use but there is maybe a solution for this.

Have you tried ViewStateModeByIdAttribute?

You may need a custom container control which use this attribute and is an implementatiion of INamingContainer.

mo.
  • 3,474
  • 1
  • 23
  • 20