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.
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.
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.
Then in the target page I did the following changes.
In the HTML section I added the @PreviousPageType directive as follows.
Then in the .CS file I can directly access the SourcePage Control as follows.
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.
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