gpt4 book ai didi

c# - 为什么不能在 Page.PreInit 事件之后动态应用 Themes 和 Master pages?

转载 作者:行者123 更新时间:2023-11-30 21:23:09 25 4
gpt4 key购买 nike


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

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

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


Page.PreInit 后Themes 设置不上还有其他原因吗?


2)另外,为什么Page.PreInit之后不能应用Master pages?

谢谢

最佳答案

据此:

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

文章还包括这个例子:

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";
}

关于c# - 为什么不能在 Page.PreInit 事件之后动态应用 Themes 和 Master pages?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1961950/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com