4. Control lifecycle

4.1 Lifecycle events

Just like in regular ASP.NET, every web page and control in SASP has a certain lifecycle between creation and destruction (garbage collection). Knowing the lifecycle is very important to know your way around developing SASP web applications. Fortunately, it's quite simple.

Every SasControl? and page goes through several states in its lifetime (in this order):

  • Created: Set after the control has been constructed.
  • PreInitialized?: Set when the executing HttpContext? of the control is set. During this state databinding is done before calling any custom pre-initialization methods.
  • Initialized: Set when the control has been 'prepared' and custom initialization methods are being called. Before calling custom initialization methods any specified resources are loaded. After initialization of the control, the initialize event is triggered for all child controls.
  • Dispatched: Set after initializing while events are being triggered (for example Click event of a button). After dispatching events for the control, the dispatch event is triggered for all child controls.
  • PreRendered?: Set after event dispatching, while specific tasks for rendering are being executed. After prerendering the control, the prerender event is triggered for all child controls.
  • Rendered: Set when the actual output of the control is being rendered.
  • PostRendered?: Set when additional methods after rendering are being executed. After postrendering the control, the postrender event is triggered for all child controls.

4.2 Lifecycle methods

Hooking into the lifecycle is easy, not by assigning delegates to events but by overriding the appropriate methods, the old-fashioned way. After constructing a control, the following lifecycle methods are meant to be overriden during development.

1. PreInitialize?()

In this method, all request bound fields are set, but no resources have been loaded. Use this method in base pages where you want to specify settings before executing the normal business logic. Basic examples are setting the CultureInfo? of a request or checking if the user is correctly authenticated.

Use: Less common, usually in base controllers to provide general settings or execute checks.

2. Initialize()

Use this method to link up events and execute actions before control events are being dispatched.

Use: Common use, usually the entry point of business logic for a control.

3. Dispatch()

Use this method to catch a custom event of your control (for example to execute a delegate if a certain request variable is set). Note that if a control is not visible when the Dispatch event is triggered, this method will not be called and no control events (also not for the children) will be dispatched.

Use: Infrequent use, only use when you need to catch specific events in a custom made control.

4. PreRender?()

Use this method to execute actions after events have been dispatched, for example to load an order line list after the order has been selected with a button Click event.

Use: Common use, use to determine display settings after event dispatching, but before the actual rendering.

5. Render( HtmlTextWriter? )

Use: Common when creating custom controls, but avoid use if possible.

6. PostRender?()

Use: Rarely used, mainly used to release resources or provide additional post-rendering logging.

The lifecycle methods are executed in block: if you load a control in the PreRender? method, and attach it to a control in that same PreRender? method. Then automatically the PreInitialize?, Initialize and Dispatch methods of the new control will be executed so it is synchronized with the other controls. Only when the Render event has passed are lifecycle methods of new controls ignored.

3. View Syntax | 5. Control tree