{infiniteZest}
// Articles. Tutorials. Utilities.
Home  |   Search  |   Login  
Categories Skip Navigation Links
New / All
AJAX
Apple
ASP.NET
.NET
Git
Google / Android
Python / IronPython
Miscellaneous
SQL Server
What happens when a custom control posts back?
Summary
This article discusses the order in which various methods are executed when a custom control posts the data back to the server.
 
Table of Contents

The IPostBackDataHandler Interface

Order of Method Execution

Figure 1. Control PostBack Sequence

Control Posted Back

LoadPostData

Event Handling From a Custom Control

 

So, you are writing a custom control. And this custom control a two-way street -- meaning, you will be presenting something to the user and the user will be doing something with it or adding something to it that you need to capture.

The IPostBackDataHandler Interface

To handle the data that the user posted back, you would need to implement IPostBackDataHandler methods in your custom control. Your custom control class will have a signature like this:

public class MyCustControl : WebControl, IPostBackDataHandler

In your code, you need to implement two methods that belong to IPostBackDataHandler:

public bool LoadPostData(string postDataKey, NameValueCollection postCollection)
public void RaisePostDataChangedEvent()

In addition, you might also implement a TextChanged event handler. Let’s see the order in which this post back handling mechanism executes.

Order of Method Execution

Following figure shows the order in which various post back handling methods execute.

Figure 1. Control PostBack Sequence

Control PostBack Sequence

Let’s look at various pieces of the above diagram:

Control Posted Back

The user has, for example, clicked some button on the page or on the control that makes the page to be posted back to the server. The server infrastructure recognizes that this particular control wants to handle its data that has been posted back.

LoadPostData

This is the first method that gets called. This method is discussed in further detail in this article: http://www.infinitezest.com/articles/loading-the-posted-data-on-the-post-back.aspx

The LoadPostData method has a bool return type. You want this method to return false, if your control state has not been changed. For example, if your custom control lets the user edit some text and no text has been changed between this and the previous postback, you would want to return false.

If your control state has been changed (for example, the user has changed the text), then you want to return true.

If this method returns false, then there is nothing else to do. The execution will go back to the page containing this control. However, if the return value is true (i.e. the control state has been changed), then the RaisePostDataChangedEvent() method will get called. From here, you can execute the OnTextChanged method.

public void RaisePostDataChangedEvent()
{
    OnTextChanged(EventArgs.Empty);
}

public event EventHandler TextChanged;

protected virtual void OnTextChanged(EventArgs e)
{
    if (TextChanged != null)
        TextChanged(this, e);
}

Event Handling From a Custom Control

The above three methods form the crux of raising an event inside a custom control that can be handled on the page where it resides. In the above TextChanged is a delegate that holds the pointer to the method that will actually get executed at runtime. You can setup the name of that delegate method declaratively, as shown below (the local method awTest_TextChanged gets called from the OnTextChanged method above):

<ajw:AJAXWrite ID="awTest" runat="server" Height="500px" Width="750px" OnTextChanged="awTest_TextChanged" />

In that local delegate method, you can do whatever you want (with the information that the text in the custom control has changed). For example, here, we might want to save that changed text to the database.

/// <summary>
/// The text has changed.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void awTest_TextChanged(object sender, EventArgs e)
{
    // Save the text to the database.
}

Bookmark and Share This

More Articles With Similar Tags
icon-scriptmanager-scriptreference.jpg
Needless to say, the JavaScript files are essential to adding AJAX functionality to ASP.NET sites or ASP.NET custom controls. If you are using ASP.NET AJAX client libraries, the ScriptManager should recognize your .js files. This article looks at how to make ScriptManager recognize your existing JavaScript files.
icon-ajax-to-existing-projects.jpg
Since JavaScript has been supported by every major web browser for a long time now, every decent-sized web site uses some JavaScript. Now that ASP.NET AJAX is available, you might want to refactor and start adding some of the functionality available in these libraries. This article goes through the initial refactoring steps.
This article shows how to embed the JavaScript files (.js) in your custom control dll. Along the way discusses the advantages of doing so.
icon-postcollection-allkeys.jpg
How to get the postback data in the individual controls in your custom control? This article explains the LoadPostDataMethod with examples and figures.
This article looks at the very first steps (from a practical consideration) after you have decided to build an asp.net custom control.
About  Contact  Privacy Policy  Site Map