获取 ASP.NET 页面生命周期的状态

发布于 2024-09-16 01:41:16 字数 246 浏览 3 评论 0原文

我的方法需要以下功能:如果在 ASP.NET 生命周期的 OnLoad 事件之前调用该方法,则抛出异常,否则继续执行该方法。

我在想这样的事情:

if (Page.LifeCycleState < LifeCycleState.OnLoad) {
    throw new InvalidPageStateException();
}

是否可以检索 ASP.NET 页面生命周期的状态?

I need the following functionality in my method: if the method is called before OnLoad event of ASP.NET life cycle throw an exception else continue execution of the method.

I was thinking of something like this:

if (Page.LifeCycleState < LifeCycleState.OnLoad) {
    throw new InvalidPageStateException();
}

Is it possible to retrieve the state of ASP.NET page life cycle?

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

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

发布评论

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

评论(4

北渚 2024-09-23 01:41:16

一种方法是使用您在站点中始终使用的基本页面。这将包含一个名为 PageLoadComplete 的变量,您可以在 PageLoad 事件结束时设置该变量。然后您可以在方法中检查该变量的状态。

public abstract class BasePage : System.Web.UI.Page
{
    public bool PageLoadComplete { get; private set; }
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        PageLoadComplete = true;
    }
}

如果您想从页面外部的代码(例如 UserControl)访问该变量,则必须将其公开并将您的页面转换为 BasePage。

public partial class MyUserControl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        BasePage basePage = this.Page as BasePage;
        if (basePage != null && !basePage.PageLoadComplete)
        {
            throw new InvalidPageStateException();
        }
    }
}

One approach would be to use a Basepage that you always use in your site. This would contain a variable called PageLoadComplete, which you would set at the end of your PageLoad event. Then you could check the state of this variable from within your method.

public abstract class BasePage : System.Web.UI.Page
{
    public bool PageLoadComplete { get; private set; }
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        PageLoadComplete = true;
    }
}

If you want to access the variable from code external to your page such as a UserControl, you would have to make it public and cast your page as BasePage.

public partial class MyUserControl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        BasePage basePage = this.Page as BasePage;
        if (basePage != null && !basePage.PageLoadComplete)
        {
            throw new InvalidPageStateException();
        }
    }
}
杯别 2024-09-23 01:41:16

System.Web.UI.Control 类的实现中有属性(实现):

    internal ControlState ControlState { 
        get { return _controlState; }
        set { _controlState = value; } 
    } 

其中 ControlState 是包含成员的枚举例如:已初始化、ViewStateLoaded、已加载等这里声明

但正如你所看到的,这个属性是内部的。因此,获得控制状态的唯一方法是 Daniel Dyson 提出的。

There is property in a realization of System.Web.UI.Control class(realization):

    internal ControlState ControlState { 
        get { return _controlState; }
        set { _controlState = value; } 
    } 

Where ControlState is enum that contains members such as: Initialized, ViewStateLoaded, Loaded etc. here declaration

But as you can see this property is internal. So only way to get control state is proposed by Daniel Dyson.

看透却不说透 2024-09-23 01:41:16

通过查看当前 HttpContextCurrentHandlerPreviousHandler 属性,您也许能够找到您要查找的内容。

You maybe able to find what you are looking for, by looking at the CurrentHandler and PreviousHandler properties of the current HttpContext.

澜川若宁 2024-09-23 01:41:16

如果该方法在 ASP.NET 生命周期的 OnLoad 事件之前调用
抛出异常,否则继续执行该方法。

目前尚不清楚 Onload 事件的含义,也不清楚“方法”所在的位置。是页面的OnLoad还是控件的OnLoad?是页面的“方法”还是控件的“方法”?

无论如何,我们可以在 Context.Items 字典中存储某种标志,所有控件(包括 Page)在请求期间都可以访问该字典。这消除了使用上面建议的通用基页的需要。

在OnLoad方法中(无论是Page的OnLoad还是Control的OnLoad):

Context.Items[UniqueID] = this;

在“方法”中:

if (Context.Items[UniqueID] != null) 
{
    throw new InvalidPageStateException();
}

if the method is called before OnLoad event of ASP.NET life cycle
throw an exception else continue execution of the method.

It is not clear which Onload event is meant, nor where the "method" resides. Is it the Page's Onload or a Control's OnLoad? Is it a Page's "method" or a Control's "method"?

Anyway, one can store sort of flag in the Context.Items Dictionary, which all controls (including Page) have access to during a request. This eliminates the need to use a general base page like suggested obove.

In the OnLoad method (no matter whether it is a Page's OnLoad or a Control's OnLoad):

Context.Items[UniqueID] = this;

In the "method":

if (Context.Items[UniqueID] != null) 
{
    throw new InvalidPageStateException();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文