asp.net: Viewstate and dynamically added controls

start with this article from Scott Mitchell

Understanding ASP.NET View State


WINDOWS\Microsoft.NET\Framework\Version\Temporary ASP.NET

Instantiation, 
Initialization, 
Load View State, 
Load, 
Save View State

All controls init method gets called

If we need our dynamically added controls to maintain their view state it is paramount that these controls be added before the Load View State stage. That is, these controls must exist within the page's control hierarchy before the view state is loaded. There's only one stage before Load View State - Initialization. That means, if we want our dynamic controls to persist view state we must add them to the control hierarchy in the page's Init event.

Working with Dynamically Created Controls

ASP.NET Page Life Cycle Overview

When an ASP.NET page runs, the page goes through a life cycle in which it performs a series of processing steps. These include initialization, instantiating controls, restoring and maintaining state, running event handler code, and rendering. It is important for you to understand the page life cycle so that you can write code at the appropriate life-cycle stage for the effect you intend. Additionally, if you develop custom controls, you must be familiar with the page life cycle in order to correctly initialize controls, populate control properties with view-state data, and run any control behavior code. (The life cycle of a control is based on the page life cycle, but the page raises more events for a control than are available for an ASP.NET page alone.)

HOW TO: Dynamically Create Controls in ASP.NET with Visual Basic .NET


Private Sub 
Page_Init(ByVal sender As System.Object, 
     ByVal e As System.EventArgs) 
Handles MyBase.Init

    ' Create dynamic controls here.
    TextBox1 = New TextBox()
    TextBox1.ID = "TextBox1"
    TextBox1.Style("Position") = "Absolute"
    TextBox1.Style("Top") = "25px"
    TextBox1.Style("Left") = "100px"
    Form1.Controls.Add(TextBox1)

    TextBox2 = New TextBox()
    TextBox2.ID = "TextBox2"
    TextBox2.Style("Position") = "Absolute"
    TextBox2.Style("Top") = "60px"
    TextBox2.Style("Left") = "100px"
    Form1.Controls.Add(TextBox2)

    ' CODEGEN: The Web Form Designer requires this method call.
    ' Do not modify it by using the code editor.
    InitializeComponent()
End Sub

Understanding the Page Life Cycle in ASP.NET - Joydip Kanjilala


Page_Init 
LoadViewState 
LoadPostData 
Page_Load 
RaisePostDataChangedEvent 
RaisePostBackEvent 
Page_PreRender 
SaveViewState 
Page_Render 
Page_UnLoad

protected override void OnInit(EventArgs args) { 
 
this.lstStates.DataSource = QueryDatabase(); 
 
this.lstStates.DataBind(); 
  
base.OnInit(e); 
 
}

which is better oninit or page_init?

Event Handling in ASP.NET...

Each class that publishes an event also provides a protected, virtual method that raises the event. When the specific incident occurs the publisher class invokes this method. The task of this method is to notify all the event subscribers about the event. The event raising methods are identifiable via their naming convention ? the word 'On' followed by the name of the event, e.g. OnPreRender.