views:

58

answers:

3

Is there any way to check what events have fired during the asp.net webforms page/control lifecycle?

I know that I can manually add handlers for each event, but that seems a bit ... inefficient.

Is there a visualiser, or a property that I can check that will tell me whether these events have fired?

EDIT The reason I want to know this is that I am overriding the ViewState property of a custom control, and the viewstate disappears at some point, and I'd like to know at which point in the page lifecycle it is being overriden.

A: 

The way events work in .NET is that you register you interest in them, and the framework will call your handler when they are fired. I'm not aware of any visualiser or property that tells you where you are in the life cycle, other than knowing where you are.

On MSDN, ASP.NET Page Life Cycle Overview gives a pretty good description of what happens when, and as everything you do in the page or on a control will be as part of handler of one of those events, you should be able to work out where in the process you are.


Edit to add

The SaveStateComplete event is raised once all controls etc have finished saving their details to the ViewState - this is where your code should probably go.

Controls can make changes to the ViewState up to that point - and generally will be doing so in the PreRender event (which fires just before SaveStateComplete).

What sort of changes are you making?

Zhaph - Ben Duguid
A: 

Page and control events fire in a very strict, very predictable fashion. The Page and all controls handle one event before moving on to the next one. So if you put your code in the Page, you can know exactly what has happened in all associated controls.

However, overriding ViewState directly isn't safe; controls depend on it. Instead, you might look at overriding SavePageStateToPersistenceMedium() and LoadPageStateFromPersistenceMedium() on the Page; they give you a lot of control over the ultimate disposition of ViewState.

In case it helps, I cover those methods and other related optimizations in my book: Ultra-Fast ASP.NET.

RickNZ
Thanks for the tips about viewstate, but I'm really just wanting to know how to determine what events have fired on Page or UserControl at a given point in time...
Khanzor
What do you mean by "at a given point in time"? Usually, that would correspond to some code, and that code would live in an event handler, and the corresponding event for that handler would dictate which other events have already fired.
RickNZ
Well, if I've overridden a property, I want to know what event had been raised in the page to call into that property. This is really just to save me specifying each override in the page and setting a break point in each of these events to see what event the page has raised.
Khanzor
One approach would be to capture a stack trace in the property, using `Environment.StackTrace` or `System.Diagnostics.StackTrace`. Another approach would be to have a base class for your page that you apply only for pages that you want to diagnose, and have it override all of the event methods and call `System.Diagnostics.Debugger.Break()` to force a breakpoint when the code is hit. That way, it would be easy to enable and disable.
RickNZ
A: 

Once again, I'm answering my own question.

What I wanted to do was to determine what events had fired in the page life cycle.

This is actually pretty trivial: Enable tracing in the web.config, and then go to ~/Trace.axd to see what had fired.

Khanzor