CrossPagePostBack Insights


By default any ASP.NET page will post back to itself. But in ASP.NET 2.0 you can configure the page to post back to a different page. This behavior is known as CrosspagePostBack. There are 2 steps involved in configuring this behavior.

1. Identify the controls that can cause PostBack. Example: Button Controls (in particular, those that implement the IButtonControl)

2. Set the PostBackURL property of the controls to the target page.

The following code block shows how you can configure this for a button control.

image

 

When you configure this the ASP.NET runtime binds a new Javascript function WebForm_DoPostBackWithOptions instead of the normal WebForm_doPostBack for the button control. Below is the HTML rendered for the above button control.

image

How can we access any controls on the SourcePage (Default.aspx) from the TargetPage (in this case Default2.aspx)?

We can use the PreviousPage property on the TargetPage(Default2.aspx) to get access to the SourcePage (Default.aspx). In the following example I am accessing the text box control I have on the SourcePage and setting the text value on the TargetPage from the SourcePage textbox value.

Below is the code snippet for the same in Default2.aspx page.

image

There is one more way in which we can access the Sourcepage controls i.e. through the @PreviousPageType directive. If you know the target page in advance this is the best method. But for this to work you need to expose the controls as public properties in the SourcePage(Default.aspx)

So I rewrite my code by exposing the textbox control as follows.

image

 

Then in the target page I did the following changes.

In the HTML section I added the @PreviousPageType directive as follows.

image

Then in the .CS file I can directly access the SourcePage Control as follows.

image

Now comes the interesting part. How can you identify whether a Page has been rendered as a part of normal request or is it due to a CrossPagePostback?

We have a property called IsCrossPagePostback. This tells whether the current page has called any other page. So to identify whether the current page has been called by the CrossPagePostback we have to use a combination of PreviousPage property (I discussed above) and this property.

So to identify whether Default2.aspx has been called due to CrossPagePostback I use the combination as follows.

image

 

So I you reached the TargetPage (Default2.aspx) by means of CrossPagePostBack you will see the text box value the same as the SourcePage. If you browsed the TargetPage independently you will see its value as “This is a normal request”.

I hope this article will help you

Advertisements
This entry was posted in Interesting Stuff. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s