0

我在 Global.asax 中使用此代码更改母版页:

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;

每当更改母版页时,内容页中所有控件的视图状态都会丢失。我想知道如何保存它们...

4

1 回答 1

1

更改母版页确实会影响生成的控制结构。因此 ASP.NET 无法加载视图状态,因为它使用控制索引来执行此操作。

我不知道您使用的是哪个版本的 .NET,但可能有解决方案。

你试过ViewStateModeByIdAttribute吗?

您可能需要一个自定义容器控件,该控件使用此属性并且是INamingContainer.

于 2013-04-19T13:08:30.833 回答