Reduce Repetitive Code with ASP.NET Custom Controls - Handling ViewState and Post Back Events
(Page 4 of 6 )
Managing the ViewState and handling postback events are some of the most confusing and difficult things to do in ASP.NET.
When working with pages that postback, its very important to remember that ASP.NET needs to be able to recreate the page and all of its controls in memory on the server before attempting to recreate the ViewState or fire events for server controls.
If the page cannot be reconstructed properly, then ASP.NET may have difficulty determining which events to fire, and as a result your pages may not work properly.
A good rule of thumb is to make sure that the page and its controls can be “rebuilt” by the time that the OnLoad method for the page is called.
Let’s look at an example.
In order to allow your control to automatically display the Contacts data and persist that data when the page is reposted, you’ll need to override the OnInit and OnLoad methods for the control.
The OnInit and OnLoad events for our custom control are shown below:
protected override void OnInit(System.EventArgs e)
{
//If data is not being auto loaded then do not do anything here
if(!_autoLoadData)
return;
//Otherwise, perform databinding
if(base.EnableViewState)
{
//If using ViewState, only DataBind the FIRST time the page is loaded.
if(!Page.IsPostBack)
this.DataBind();
}
else
{
//If not using ViewState, DataBind EVERY time the page is loaded.
this.DataBind();
}
}
protected override void OnLoad(System.EventArgs e)
{
if(base.EnableViewState && _autoLoadData)
base.DataBind();
}
After looking at this code, you may be wondering why there is a need to databind in both the OnInit and OnLoad methods when using the ViewState and attempting to automatically load the Contacts data.
Simply put, you must databind in OnInit to ensure that there is data available when the OnLoad event is fired for your ASP.NET page.
Additionally, you must databind again in the OnLoad method of the control to ensure that the Contacts data is persisted into the ViewState.
A short discussion of the ASP.NET page lifecycle can help illustrate why these actions need to be taken.
ASP.NET will first fire the OnInit events for all server controls, and then the OnInit for the ASP.NET page itself. Then, the OnLoad event is fired for the ASP.NET page, followed by the OnLoad events for any server controls.
After that, ASP.NET will parse the ViewState and determine if any events need to be fired for specific controls.
But that’s not the whole story. In between the firing of the OnInit and OnLoad events, there are in fact several more methods that handle various tasks when a page or control is created.
However, for this example, we’re concerned with only four: OnInit, LoadViewState, OnLoad and SaveViewState.
For a control, these methods are called in the order listed above, and you can override any one of them to perform custom actions if need be.
While you can databind a control at any point in the control creation lifecycle, the underlying data will not be persisted to the ViewState unless the operation occurs after the call to LoadViewState, but before the call to SaveViewState.
Any ViewState operations performed either before or after these events will not be persisted into the ViewState by ASP.NET, hence the need for databinding in both the OnInit and OnLoad methods.
Next: Implementing in an ASP.NET page >>
More ASP.NET Articles
More By Rich Sbarro