Get ASP.NET Page Lifecycle State

I need the following functionality in my method: if the method is called before the OnLoad ASP.NET lifecycle event, throw an exception, the rest will continue the method.

I was thinking of something like this:

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

Can I restore the lifecycle state of an ASP.NET page?

+4
source share
4 answers

One approach is to use the base page that you always use on your site. This will contain a variable called PageLoadComplete that you would set at the end of your PageLoad event. Then you can check the state of this variable inside 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 a variable from code external to your page, such as UserControl, you need to make it public and discard 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(); } } } 
+4
source

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

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

Where ControlState is an enumeration that contains elements such as: Initialized, ViewStateLoaded, Loaded, etc. here is the declaration

But, as you can see, this property is internal. Thus, Daniel Dyson proposed only a way to obtain a control state.

+1
source

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

0
source

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

It is not clear which Onload event is implied and where the "method" is located. Is this an Onload or Control OnLoad page? Is this a Page method or a Management Method?

In any case, in the Context.Items dictionary you can store some flag to which all controls (including the page) have access at the time of the request. This eliminates the need for a common base page, such as the one suggested above.

In the OnLoad method (regardless of whether it is an OnLoad or Control OnLoad page):

 Context.Items[UniqueID] = this; 

In the "method":

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

All Articles