2013-01-20 32 views
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; 

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

回答

1

更改主页面会影响生成的控制结构。 因此,ASP.NET无法加载视图状态,因为它使用控件指示来执行此操作。

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

您可能需要使用此属性的自定义容器控件,该控件的实现方式为INamingContainer

相关问题