Cross Page Posting in Asp.NET

In Simple words Cross page posting means allow a page to cross post / submit to another page ,

Using cross page posting we can access the control values from the previous page(source Page) i.e. from which it is get posted.

Although there are different methods like QueryString ,Server.Transfer ,Response.Redirect ,Session Objects,cookies are available for passing data between two pages,there are certain/specific cases such as filling multiple entry form in which we need to have the multiple entry data on the previous forms for some reference , in cases like mentioned we can specifically use Cross Page Posting,other mentioned methods are not so useful to use in these cases.

We can use the server controls ,which are implemented from IButtonControl interface e.g. Button ,LinkButton,ImageButton etc.

I am using one simple example to explain how to use ‘PostBackUrl’ property

Lets say we are creating one Car Insurance application and on the first page we are taking First name, Last name,Date & COuntry from the user and then we are forwarding to the user to next page for further filling out further details and creating the query using the filled data. In this case we can use.

PostBackUrl functionality following steps.

1) Set the the ‘Next’ button’s property ‘PostBackUrl’ to “~/PostBack2.aspx” or the page which is your next page. check the following sample page with postbackurl on ‘Next’ button.

PostBackUrl1

<asp:Button ID=”btnNext” runat=”server” PostBackUrl=”~/PostBack2.aspx” Text=”Next” Width=”97px” />

2)On the next page if you need to check whether the page was coming through cross-page posting or from other like Server.Transfer,Response.Redirect operation,The .Net provides new page property called IsCrossPagePostBack.

We need to check it like following

if (PreviousPage.Page.IsCrossPagePostBack)

{

}

3) We can get the values entered by user on previous page using FindControl(). In Our case it will look like as given below.

if (PreviousPage.Page.IsCrossPagePostBack)

{

TextBox tbxFirstName = (TextBox)PreviousPage.FindControl(“tbxFirstName”);

TextBox tbxLastName = (TextBox)PreviousPage.FindControl(“tbxLastName”);

TextBox tbxDate = (TextBox)PreviousPage.FindControl(“tbxDate”);

DropDownList ddlCountry = (DropDownList)PreviousPage.FindControl(“ddlCountry”);

lblMessage.Text = tbxFirstName.Text + ” ” + tbxLastName.Text;

} We can see values from First & second textbox retrieved on the second page as in below image.

PostBackUrl2

Note : While using Master pages,we need to find the previous page control at the 2 levels deep , First we need to find out ContentPlaceHolder of the masterpage and then we need to find the control from ContentPlaceHolder

e.g.

(TextBox)PreviousPage.Master. FindControl(“ContentPlaceHolder1”). FindControl(“tbxFirstName”);

Detail Ref – MSDN

CrossPagePosting Property Values

When page Default.aspx posts to page PostBack2.aspx then at PostBack2 ( @ Page Load) we can find following property values

  • this.Page.IsPostBack is false
  • this.Page.PreviousPage.IsPostBack is true
  • this.Page.IsCrossPagePostBack is false
  • this.Page.PreviousPage.IsCrossPagePostBack is true


& here this.Page.PreviousPage has a reference to page Default.aspx


Advertisements

4 thoughts on “Cross Page Posting in Asp.NET”

  1. hello! one here im truly to death that after making dwarf up on i found this place.I was browsing this forum on a while without registration and today i definite to register.im glad that everybody are so nice.
    I beetle love it and i desire crack at dish out some swiftly a in timely fashion posting some usefull dope

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