Cross Page Postback in ASP.NET

Cross Page Postback in ASP.NET

This is a new feature in ASP .NET 2.0. The IButtonControl Interface contains a new property called PostBackUrl which points to the page to which the current page will postback, Button, ImageButton and LinkButton implements this interface and exposes the cross page postback functionality.

When user clicks the button in the current page will postback to the specified page which can access the source page controls through Page.PreviousPage property which returns the reference of previous page, once got the reference of previous page you can use the FindControl method to get the reference of particular or you can expose public properties from source page to provide the type safe access.

For example:

Label1.Text =((TextBox)this.PreviousPage.FindControl("TextBox1")).Text;

In order to provide the strongly typed access to previous page, you can specify the previous page in PreviousPageType directive; since you have strongly typed reference of previous page you can now easily access its public members without any typecasting.

Example:

Create two aspx pages name the first one step_1.aspx and the other step_2.aspx
In the page step_1.aspx put a Text Box named TextBox1
Expose the following properties in code behind file:

public TextBox MyName { get { return TextBox1; } }

In the step_2.aspx put this code in the aspx code:

<%@ PreviousPageType VirtualPath="~/step_1.aspx" %>

Then in the page load event in step_2.aspx page write this code:

Response.Write(this.PreviousPage.MyName.Text);

Share this post

Comments (7)

  • Tanvir Faraj Reply

    Why do i need to use “PreviousPageType” directive? Is it mandatory to use it?

    April 23, 2009 at 4:41 PM
    • Amr Saafan

      Dear Tanvir,
      You don’t have to use the “PreviousPageType” directive to be able to use cross page postback but in order to provide the strongly typed access to previous page, you can specify the previous page in PreviousPageType directive.

      April 25, 2009 at 2:58 PM
  • Tracie Vance Reply

    Beautiful blog! I discovered it although surfing around about Yahoo Information. Do you have any tips on how to acquire listed in Yahoo News? We’ve been ready for for a while nonetheless I almost never seem to arrive! Many thanks

    January 12, 2010 at 5:41 PM
  • Karen Hart Reply

    I love what you guys are usually up too. Such clever work and reporting! Keep up the awesome works guys I’ve you guys to our blogroll.

    January 13, 2011 at 5:18 PM
  • Alfred Dean Reply

    I like the valuable info you provide in your articles. I will bookmark your weblog and check again right here frequently. I am slightly sure I’ll be informed many new stuff proper here! Good luck for the next!

    June 13, 2012 at 5:18 PM
  • Constance Myer Reply

    This website was… how do you say it? Relevant!! Finally I have found something which helped me. Kudos!

    July 13, 2015 at 5:17 PM
  • Cyndi Celestine Reply

    Your means of describing everything in this article is in fact pleasant, everyone be able to simply know it, Thanks a lot.

    August 29, 2021 at 11:55 AM

Leave a Reply

Your email address will not be published.