Home arrow ASP.NET arrow Page 4 - Reduce Repetitive Code with ASP.NET Custom Controls

Reduce Repetitive Code with ASP.NET Custom Controls

In this article Rich explores the world of ASP.NET Custom Controls and how they can dramatically simplify development of ASP.NET web applications.

Author Info:
By: Rich Sbarro
Rating: 5 stars5 stars5 stars5 stars5 stars / 57
January 08, 2003
  1. · Reduce Repetitive Code with ASP.NET Custom Controls
  2. · Creating the Control
  3. · Databinding to the Control
  4. · Handling ViewState and Post Back Events
  5. · Implementing in an ASP.NET page
  6. · Conclusion

print this article

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

//Otherwise, perform databinding
//If using ViewState, only DataBind the FIRST time the page is loaded.
//If not using ViewState, DataBind EVERY time the page is loaded.

protected override void OnLoad(System.EventArgs e)
if(base.EnableViewState && _autoLoadData)

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.
blog comments powered by Disqus

- How Caching Means More Ca-ching, Part 2
- How Caching Means More Ca-ching, Part 1
- Reading a Delimited File Using ASP.Net and V...
- What is .Net and Where is ASP.NET?
- An Object Driven Interface with .Net
- Create Your Own Guestbook In ASP.NET
- HTTP File Download Without User Interaction ...
- Dynamically Using Methods in ASP.NET
- Changing the Page Size Interactively in a Da...
- XML Serialization in ASP.NET
- Using Objects in ASP.NET: Part 1/2
- IE Web Controls in VB.NET
- Class Frameworks in VB .NET
- Cryptographic Objects in C#: Part 1
- Sample Chapter: Pure ASP.Net

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials