FREE Subscription to Dr. Dobb’s Digest: Same Great Content, New Digital Edition
Site Archive (Complete)
Windows/.NET
Email
Print
Reprint

add to:
Del.icio.us
Digg
Google
Furl
Slashdot
Y! MyWeb
Blink
March 09, 2006
The IPostBackDataHandler Interface

Dino Esposito
Server controls implementing the IPostBackDataHandler interface are a key part of ASP.NET's postack mechanism.

The postback mechanism is one of the pillars of ASP.NET. When a request arrives, the ASP.NET runtime matches the ID of each posted value with a server-side control. For each match found, the ASP.NET runtime invokes a couple of methods on the matching server control. The methods invoked come from a a well-known interface—the IPostBackDataHandler interface.

By implementing the IPostBackDataHandler interface each server control gets a chance to update its current state with data posted by the client. The interface lists two methods—LoadPostData and RaisePostDataChangedEvent.

The ASP.NET runtime invokes LoadPostData on each control to give the control a chance to update its current state with the data coming from the client. Here's the method's signature:

public bool LoadPostData(
    string postDataKey, 
    NameValueCollection postCollection);
The first argument of LoadPostData is the client ID of the control; the second argument is a name-value collection that contains all the data posted from the client. Note that when the ASP.NET runtime gets to process a request, it first copies the posted data from either Request.Form or Request.QueryString into a helper collection. This helper collection is then passed to LoadPostData.

Each control locates the entry in the collection whose key matches the ID of the control. The value associated with the entry, if any, is used to update the value of one or more control properties. For example, input text boxes compare the posted value to the Value properties; check boxes, instead, compare it to the Checked property. The posted value is obtained using the postDataKey parameter as a key to access the values stored in the collection.

Controls that implement the IPostBackDataHandler interface use a boilerplate piece of code to implement the LoadPostData method. Basically, the method updates the key property of the control with the posted value. The code below shows how LoadPostData works for the HtmlInputText control.

bool LoadPostData(string postDataKey, NameValueCollection postColl) 
{
   string oldValue, postedValue;

   // Cache the current value of the property 
   oldValue = this.Value;

	// Get the posted value for the HTML element with the 
   // same ID as the control		
   postedValue = postColl[postDataKey];

   // Compare the posted value with Value and updates if needed
   if (oldValue != postedValue) 
   {
	   this.Value = postedValue;
      return true;
   }
     
	// Indicates whether the state has changed
   return false;
}
LoadPostData is expected to return true if the state of the control changed to incorporate the posted values. For this infrastructure to work, it is key that a one-to-one correspondence exists between the ID of server controls and one client HTML element. The ASP.NET runtime tracks all the controls that return true to LoadPostData and then invokes the RaisePostDataChangedEvent method for each of them—the second method on the IPostBackDataHandler interface. The following code snippet reports what that method does for the HtmlInputText control:

void RaisePostDataChangedEvent() 
{
    this.OnServerChange(EventArgs.Empty);
}

The page that hosts the HtmlInputText control receives a ServerChange control for each hosted instance. A similar behavior is provided by all server controls that implement the IPostBackDataHandler interface. The name of the event may change, however.


Dino Esposito is Wintellect's ADO.NET and XML expert, and a trainer and consultant based in Rome, Italy. Dino is a contributing editor to Windows Developer Network and MSDN Magazine, and the author of several books for Microsoft Press including Building Web Solutions with ASP.NET and ADO.NET and Applied XML Programming for .NET. Contact Dino at dinoe@wintellect.com.


TOP 5 ARTICLES
No Top Articles.
DR. DOBB'S CAREER CENTER
Looking for a new job? open | close
Search jobs on Dr. Dobb's TechCareers
Function:

Keyword(s):

State:  
  • Post Your Resume
  • Employers Area
  • News & Features
  • Blogs & Forums
  • Career Resources

    Browse By:
    Location | Employer | City
  • Most Recent Posts:



    MICROSITES
    FEATURED TOPIC

    ADDITIONAL TOPICS

    INFO-LINK



     




    Techweb
    Informationweek Business Technology Network
    InformationweekInformationweek 500Informationweek 500 ConferenceInformationweek AnalyticsInformationweek Events
    Informationweek MagazineGlobal CIOIWK Government ITbMightyByte and SwitchDark Reading
    Digital LibraryIntelligent EnterpriseInternet EvolutionNetwork ComputingPlug Into The CloudDr. DobbsContentinople
    space
    TechWeb Events Network
    InteropVoiceConWeb 2.0 ExpoWeb 2.0 SummitEnterprise 2.0Mobile Business ExpoNoJitter
    Black HatGTECEnergy CampCloud ConnectGov 2.0 ExpoGov 2.0 Summit
    space
    Light Reading Communications Network
    Light ReadingLight Reading AsiaUnstrungCable Digital NewsInternet EvolutionPyramid Research
    Heavy ReadingLight Reading LiveLight Reading InsiderEthrnet ExpoTelco TVTower Technology Summit
    space
    Financial Technology Network
    Advanced TradingBank Systems and TechnologyInsurance and TechnologyWall Street and TechnologyAccelerating WallstreetBST SummitBuyside Trading SummitIT Summit
    space
    Microsoft Technology Network
    MSDNTechNetTotal IT ProTotal Dev ProNET Total Dev Pro CommunitySQL Total Dev Pro Community
    space