an ASP.NET, C# technical blog, by Gianni Tropiano

Understanding dynamic controls - part 1 - Page LifeCycle

by CodeGolem 23. April 2009 23:48

Creating controls at runtime and adding them dynamically on the page is one of the most interesting features in an ASP.NET application.

But before creating and using such dynamic controls one should be aware they must be handled some more carefully then static ones.

First thing to keep in mind is how an ASP.NET page works: how it is created-rendered-destoyed at each postback.
This is what we call the page's lifecycle.

At each roundtrip the page goes through the following stages:

  • Request
  • Initialization (static controls are instantiated, themes are applied)
  • Load (if the request is a postback, control states arerecovered here)
  • Validation (IsValid property is updated here depending on the result of validators on the page)
  • Event handling (if the request is a postback, event handlers are invoked here)
  • Rendering
  • Unload

This process is translated into this sequence of events on the Page object:

After the page is rendered as HTML document and transmitted to the client, it is unloaded from the server.
Memory and other resources are freed up.

So, what happens to dynamic controls we have instantiated, say, in the page's Load event?
They are destroyed!

This explains one of the most recurring questions: "Why do my dynamic controls disappear?".

One could also ask "Why do static controls not?".

Well... they are destroyed as well... but re-created automatically on the next roundtrip.

This is the point: static controls are re-created automatically, where dynamic ones should be re-created by our code.

We will go through this process in the next part.

Tags: ,

ASP.NET

Add comment



  Country flag

biuquote
  • Comment
  • Preview
Loading



Sponsored by

LiveCo®
Silverlight® based
video conferencing platform

Hosting provided by

Lineadigitale