为什么不能在 Page.PreInit 事件之后动态应用主题和母版页?

发布于 2024-08-16 05:56:23 字数 379 浏览 5 评论 0原文


1) 我认为主题只能在 Page.PreInit 事件处理程序中以编程方式设置,原因如下:

  • 如果我们在 Page.Init 事件处理程序中设置主题,那么到那时 ViewState 就已经是跟踪,因此 Theme 应用的任何数据都会被跟踪并标记为脏数据(这会消耗大量带宽)?

  • 如果我们在 Init 事件之后设置它,那么主题还可以覆盖应用于各个控件的反序列​​化 ViewState 数据吗?


还有其他原因导致Page.PreInit后无法设置主题吗?


2)另外,为什么Page.PreInit之后不能应用母版页?

谢谢

1) I assume Themes can be set programatically only inside Page.PreInit event handler due to the following reasons:

  • if we’d set a Theme inside Page.Init event handler, then by that time ViewState would already be tracked and thus any data applied by Theme would be tracked and marked as dirty ( which would consume lot of bandwidth ) ?

  • and if we’d set it after Init event, then Themes could also override deserialized ViewState data applied to individual controls?

Are there any other reasons why Themes can’t be set after Page.PreInit?

2) Also, why can't Master pages be applied after Page.PreInit?

thanx

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜深人未静 2024-08-23 05:56:23

据此:

http://odetocode.com/articles/450.aspx

“MasterPageFile”属性只能
在“Page_PreInit”中或之前设置
事件。

这个例外是有道理的,因为我们
知道母版页必须重新排列
之前页面的控件层次结构
Init 事件触发

本文还包括这个示例:

using System;
using System.Web.UI;

public class BasePage : Page
{
   public BasePage()
   {
        this.PreInit += new EventHandler(BasePage_PreInit);
   }

    void BasePage_PreInit(object sender, EventArgs e)
    {
        MasterPageFile = "~/Master1.master";
    }
}

或者,我之前使用过的一种方法:

protected override void OnPreInit(EventArgs e) 
    { 
        base.OnPreInit(e); 
        if (Request.QueryString["Master"] == "Simple") 
            MasterPageFile = "~/Masterpages/Simple.Master"; 
    } 

According to this:

http://odetocode.com/articles/450.aspx

The 'MasterPageFile' property can only
be set in or before the 'Page_PreInit'
event.

This exception makes sense, because we
know the master page has to rearrange
the page’s control hierarchy before
the Init event fires

The article also includes this example:

using System;
using System.Web.UI;

public class BasePage : Page
{
   public BasePage()
   {
        this.PreInit += new EventHandler(BasePage_PreInit);
   }

    void BasePage_PreInit(object sender, EventArgs e)
    {
        MasterPageFile = "~/Master1.master";
    }
}

Or, an approach I've used before:

protected override void OnPreInit(EventArgs e) 
    { 
        base.OnPreInit(e); 
        if (Request.QueryString["Master"] == "Simple") 
            MasterPageFile = "~/Masterpages/Simple.Master"; 
    } 
丘比特射中我 2024-08-23 05:56:23

主题还有其他原因吗
Page.PreInit之后不能设置吗?

是的。主题包括皮肤,它可以指定控件的属性。这些属性需要在 Init 事件期间设置,因此需要在此之前选择所需的主题。

ViewState 跟踪可能是一个问题,但我认为与上述问题相比,这是一个小问题。

请注意,StyleSheetTheme(在我看来,优于常规主题)实际上是从页面上的重写属性设置的,而不是通过设置属性本身的值(除非您从 HttpModule 设置它)。

为什么不能应用母版页
在Page.PreInit之后?

控件根据其在控件树中的位置(包括访问表单控件等)来确定其 ID 以及各种其他特征和属性。母版页相当于一组父控件,因此控件可以完全初始化自身,直到父结构就位。初始化发生在 Init 事件期间,因此需要在此之前选择母版页。

Are there any other reasons why Themes
can’t be set after Page.PreInit?

Yes. Themes includes skins, which can specify properties for controls. Those properties need to be set during the Init event, so the desired theme needs to be selected before then.

ViewState tracking may be an issue, but I think it's a minor one compared to the above.

Note that a StyleSheetTheme (preferable to a regular Theme, IMO), is actually set from an overridden property on the Page, not by setting the value of the property itself (unless you set it from an HttpModule).

why can't Master pages be applied
after Page.PreInit?

Controls determine their IDs and various other characteristics and properties based on their location in the control tree (including things like accessing the form control, etc). A Master Page acts as what amounts to a set of parent controls, so controls can fully initialize themselves until that parent structure is in place. Initialization happens during the Init event, so the Master Page needs to be selected before then.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文