4

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?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Petr Kozelek
  • 1,126
  • 8
  • 14
  • 1
    i don't know if there is a built in state indicator for asp.net pages..but alternatively you can create an enum named 'LifeCycleState' with the state properites and put a varible to hold state with this enum type on page and by catching events on page like onload,oninit you can set the currentstate value.... – dankyy1 Aug 25 '10 at 08:27
  • Yes, that is what I was thinking about if there is no such built-in functionality. – Petr Kozelek Aug 25 '10 at 08:29
  • 1
    Remember YAGNI (you aint gonna need it). Only develop the functionality that you are definitely going to need. There is no point in maintaining the state right through the page lifecycle if you are only interested in whether one page event has been reached. Otherwise you will increase the amount of testing and potential for bugs. The page lifecycle is complex once you consider user controls, MasterPages and control events. Please consider this before writing such an enumeration. – Daniel Dyson Aug 25 '10 at 08:44
  • True, true. In this particular case I need to detect that different page states were reached across the whole application. The example above was just simplification of my needs. – Petr Kozelek Aug 25 '10 at 08:55
  • In that case, the enumeration is a good idea. Just use it instead of the bool property in my answer. Make sure you have a full understanding of the life cycle. There is a good diagram half way down this page: http://msdn.microsoft.com/en-us/library/ms178472.aspx – Daniel Dyson Aug 25 '10 at 09:44

4 Answers4

4

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();
        }
    }
}
abatishchev
  • 98,240
  • 88
  • 296
  • 433
Daniel Dyson
  • 13,192
  • 6
  • 42
  • 73
1

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.

chapluck
  • 579
  • 3
  • 12
0

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();
}
Don P
  • 173
  • 11
0

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

leppie
  • 115,091
  • 17
  • 196
  • 297