ASP.NET
  Home arrow ASP.NET arrow Page 4 - Reduce Repetitive Code with ASP.NET Custom...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET

Reduce Repetitive Code with ASP.NET Custom Controls
By: Rich Sbarro
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 18
    2003-01-08

    Table of Contents:
  • Reduce Repetitive Code with ASP.NET Custom Controls
  • Creating the Control
  • Databinding to the Control
  • Handling ViewState and Post Back Events
  • Implementing in an ASP.NET page
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More ASP.NET Articles
    More By Rich Sbarro


     

    ASP.NET ARTICLES

    - 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






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT