balaweblog
balaweblog

Reputation: 15470

ASP.net page life cycle

I am having an ASP.net page in my page i am having this as my code behind files. on first access the page the page preinit, init, load methods are called. on postbacks the preinit, init, load methods are called.

My question is LoadViewstate and control state events (Overridden methods) are not firing after postbacks also

protected override void OnPreInit(EventArgs e)
{
    base.OnPreInit(e);
}
protected override void LoadViewState(object savedState)
{

    base.LoadViewState(savedState);

}

protected override void LoadControlState(object savedState)
{
    base.LoadControlState(savedState);
}
protected void Page_Init(object sender, EventArgs e)
{

}
protected void Page_Load(object sender, EventArgs e)
{
  //  lblName.Text = ViewState["Test"].ToString();
}

Upvotes: 1

Views: 1664

Answers (2)

littlegeek
littlegeek

Reputation:

This method is used primarily by the .NET Framework infrastructure and is not intended to be used directly from your code. However, control developers can override this method to specify how a custom server control restores its view state. For more information, see ASP.NET State Management Overview.

The LoadViewState method restores the view-state information that was saved during a previous SaveViewState request. The WebControl class overrides the base LoadViewState method to handle the ViewState, Style, and Attributes properties.

Also note

Control State Sometimes you need to store control-state data in order for a control to work properly. For example, if you have written a custom control that has different tabs that show different information, in order for that control to work as expected, the control needs to know which tab is selected between round trips. The ViewState property can be used for this purpose, but view state can be turned off at a page level by developers, effectively breaking your control. To solve this, the ASP.NET page framework exposes a feature in ASP.NET called control state.

The ControlState property allows you to persist property information that is specific to a control and cannot be turned off like the ViewState property.

Asp.Net StateManagement link

If your control is a customer server control take a look at

iStateManager

And for the complete overview of viewstate - had to search my bookmarks try

Truly understanding viewstate

Upvotes: 3

Gaspar Nagy
Gaspar Nagy

Reputation: 4527

ASP.NET optimizes this call, and calls the LoadViewState only if there is any custom data written to the view state.

If you set something to the view state in the first call (e.g. ViewState["foo"] = 42;), the LoadViewState will be called in the next (and subsequent) callbacks.

Upvotes: 3

Related Questions