Asp.net 如何正确更改页面';s母版页?

Asp.net 如何正确更改页面';s母版页?,asp.net,delphi-prism,oxygene,Asp.net,Delphi Prism,Oxygene,我的ASP.NET应用程序中有两个母版页。一个用于常规使用,另一个用于打印。我使用会话参数查看应用程序当前是否处于打印模式: 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 th

我的ASP.NET应用程序中有两个母版页。一个用于常规使用,另一个用于打印。我使用会话参数查看应用程序当前是否处于打印模式:

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 begin
    p.PreInit += new EventHandler(page_PreInit)
  end
end;

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 p.Master <> nil then
    begin
      if Session['P'].ToString = '1' then
        p.MasterPageFile := '~/Print.Master'
      else
        p.MasterPageFile := '~/Site.Master'; 
    end;
end;
methodglobal.Application\u PreRequestHandlerExecute(src:System.Object;e:EventArgs);
开始
var p:System.Web.UI.Page:=System.Web.UI.Page(self.Context.Handler);
如果p为nil,则开始
p、 PreInit+=新事件处理程序(第\u页PreInit)
结束
结束;
方法Global.page_PreInit(发送方:System.Object;e:EventArgs);
开始
var p:System.Web.UI.Page:=System.Web.UI.Page(self.Context.Handler);
如果p为零,那么
如果p.Master为零,则
开始
如果会话['P'].ToString='1',则
p、 MasterPageFile:='~/Print.Master'
其他的
p、 MasterPageFile:='~/Site.Master';
结束;
结束;

我在普通页面上有一个按钮,将会话['p']设置为
'1'
,在打印母版页面上有另一个按钮,将会话['p']设置为
'0'
。现在,我的问题是,在我更改了代码中的session参数之后,页面将使用过时的母版页呈现,而不是当前的母版页。用户必须按F5键才能看到正确的页面。我的
page\u PreInit()
事件似乎是在
按钮单击()之前触发的。那么,我能做什么呢?

Page\u PreInit在任何单击事件处理程序之前运行


您是否考虑过使用面板或样式表在打印模式下呈现页面?

我最后在我的
page\u PreInit
事件中使用了
Request.Params[''事件目标]
,以确定单击的控件是否是负责在正常模式和打印模式之间切换的按钮。我的代码如下所示:

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';

对对于meRather来说,与使用带有onClick事件的按钮相比,需要花费太多的时间。您可以始终使用带有QueryString参数(例如print=1或print=0)的标准HTML控件。querystring在Page_PreInit方法的请求对象中可用。然后使用它在Page_PreInit方法的开头设置会话变量。