{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
Loading the posted data on the post back
Summary
How to get the postback data in the individual controls in your custom control? This article explains the LoadPostDataMethod with examples and figures.
 
Table of Contents

The LoadPostData Method

Figure 1: Various controls on a page posting back (postCollection.AllKeys)

Who is posting the data?

Figure 2: Various controls on a page posting back (postCollection.AllKeys) along with the current control ID

ctl00$cphCenter$awArticle:

Getting the posted data

What do you do with the posted data?

 

In the world of web, the web server has no clue of what’s going on the client side unless some infrastructure is setup such that the web server is told what’s going on with specific pieces of the UI on the client. For the built-in controls (like TextBox, etc.), the data contained in them is posted back to the web server so that it can place it back in that control when that page is refreshed.

You can do the same thing for your custom control, so that it doesn’t lose its state when the page is refreshed. For this IPostBackDataHandler is used.

The LoadPostData Method

This IPostBackDataHandler has an important method where the postback data handling action takes place.

public bool LoadPostData(string postDataKey, NameValueCollection postCollection)

The second parameter of this method contains controls on the page that are posting their data back. Here you will see the usual suspects like text boxes, drop down lists, etc. If you see postCoolection.AllKeys, you will notice all these controls posting back. The following figure shows a whole bunch of controls from a single page posting their data back.

Figure 1: Various controls on a page posting back (postCollection.AllKeys)

Figure 1: Various controls on a page posting back (postCollection.AllKeys)

As you can see above, in addition to the controls like text boxes, dropdown lists, you see some hidden controls (like __EVENTTARGET, __VIEWSTATE) posting their data back as well.

Who is posting the data?

The next thing to figure out is who is posting the data. This key is available from the postDataKey parameter of the method. You can see the name of the current control in the image below.

Figure 2: Various controls on a page posting back (postCollection.AllKeys) along with the current control ID

Figure 2: Various controls on a page posting back (postCollection.AllKeys) along with the current control ID

In the above figure you can see the value of the postDataKey (the UniqueID of the control that’s currently posting back): ctl00$cphCenter$awArticle. A UniqueID is needed for each control so that server can refer to it without any name collisions. The UniqueID works in this way:

ctl00$cphCenter$awArticle:

  • ctl00 - First instance of the control. For example, if this control is inside a repeater, individual instances of those repeaters start with ctl00, ctl01, ctl02, etc.
  • cphCenter - This is the Content Place Holder that holds the control. If it’s a repeater that’s holding this control, then id of that repeater would have been there.
  • awArticle: My custom control is called AJAXWrite. Here, the id of the instance I placed on the above form is awArticle.

Getting the posted data

You need to get the posted data for a variety of reasons: you might save that posted data in a property and update the control when the page is refreshed (i.e. saving the state). Another important reason is that the server might need the data that the user posted in order to react appropriately to it (save it in the database, show a different user interface, etc.)

If your control is posting the data, getting it is straight forward:

string postedText = postCollection[postDataKey];

The postCollection parameter is a NameValueCollection, so, pass the postDataKey as the key and you will get back the postedText.

In lot of cases, your custom control might contain several individual controls. For example, the above custom control (a web editor called AJAXWrite) might contain an IFRAME as the editor, several images as the formatting tools, and perhaps a hidden INPUT as the control that stores data that the user entered. So, in this case, you would want to get the data posted by that hidden INPUT.

For example, say you have named that INPUT as:

<INPUT type=’hidden’ id=’writeinput_’" + this.UniqueID + "’ name=’writeinput_" + this.UniqueID + "’></INPUT>"

The above line says, a hidden INPUT has been created with an id writeinput_ followed by the UniqueID for the entire control. As you have seen above, the UniqueID for the entire control here is ctl00$cphCenter$awArticle. So the above INPUT will have an ID writeinput_ctl00$cphCenter$awArticle.

This way, id of the INPUT within the AJAXWrite will be unique (because the id of the entire control is unique on the page and I made sure no other control within this AJAXWrite is prefixed with writeinput_. So, if you want the data posted by an control that’s embedded within your custom control, you can use something like this:

string postedText = postCollection["writeinput_" + this.UniqueID];

If you look in the list of controls in the postCollection, you will see the above control.

What do you do with the posted data?

As mentioned above, you will use the posted data for the purposes of repopulating the control or saving the posted to database, etc. As you can see in the code below, I have a property called Text on my custom control and I am updating that property with the data posted by the INPUT control, which in turn was entered by the user from the user interface.

/// <summary>
/// Get the data posted by the user.
/// </summary>
/// <param name="postDataKey"></param>
/// <param name="postCollection"></param>
/// <returns></returns>
public bool LoadPostData(string postDataKey, NameValueCollection postCollection)
{
    string presentText = Text;
    //string postedText = postCollection[postDataKey];
    string postedText = postCollection["writeinput_" + this.UniqueID];

    if (presentText == null || !presentText.Equals(postedText))
    {
        Text = postedText;
        return true;
    }
    return false;
}

And that Text public property is used by the developer using this control to get the data entered by the user. That text is, then, saved in the database for later retrieval.

Bookmark and Share This

More Articles With Similar Tags
icon-viewstate-from-GetPropertyValue.jpg
You save the customizable public properties of a custom control in the ViewState. On the postback, you read those values and reset whatever the UI controls you want to reset. This article looks at a generic way of getting and saving the properties.
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-Control-PostBack-Sequence.jpg
This article discusses the order in which various methods are executed when a custom control posts the data back to the server.
About  Contact  Privacy Policy  Site Map