Channels ▼
RSS

The IPostBackDataHandler Interface


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.



Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 

Video