Cross Page Posting in ASP.NET Web Pages

by Tech Guru| Views: 7340

What is Cross Page Posting in ASP.NET Web Pages?

What is the syntax of cross page posting in ASP.NET and C#?

Is it possible to get first ASP.Net web page Data to second ASP.NET web Page?

How can we post values of one to page to another page in ASP.NET?

Answers (1)
 
Rick Said..

By default, button controls in ASP.NET pages post back to the same page that contains the button, where you can write an event handler for the post. In most cases this is the desired behavior, but occasionally you will also want to be able to post to another page in your application.

The Server.Transfer method can be used to move between pages, however the URL doesn't change. Instead, the cross page posting feature in ASP.NET 2.0 and higher versions (ASP.NET 3.5 and ASP.NET 4.0) allows you to fire a normal post back to a different page in the application. In the target page, you can then access the values of server controls in the source page that initiated the post back.

To use cross page posting, you can set the PostBackUrl property of a Button, LinkButton or ImageButton control, which specifies the target page. In the target page, you can then access the PreviousPage property to retrieve values from the source page. By default, the PreviousPage property is of type Page, so you must access controls using the FindControl
method. You can also enable strongly-typed access to the source page by setting the @PreviousPageType directive in the target page to the virtual path or Type name of the source page.

Here is a step-by-step guide for implementing the cross-page post back using controls that implement the IButtonControl interface.

1) Create a Web Form and insert a Button control on it.

2) Set the button's PostBackUrl property to the Web Form you want to post back. For instance in this case it is "nextpage.aspx"



<asp:Button ID="Button1" runat="server" PostBackUrl="~/nextpage.aspx" Text="Post to nextpage" />



When the PostBackUrl property of the IButtonControl is set, the ASP.NET framework binds the corresponding HTML element to new JavaScript function named WebForm_DoPostBackWithOptions. The corresponding HTML rendered by the ASP.NET 2.0 will look like this:



<input type="submit" name="Button1" value="Post to Page 2"
onclick="javascript:WebForm_DoPostBackWithOptions(new
WebForm_PostBackOptions("Button1", ",falseā€,"Page2.aspx", false, false))" id="Button1" />



Getting Previous Page values on Next Page
The Page class exposes a property named PreviousPage(). If the source page and target page are in the same ASP.NET application, the PreviousPage() property in the target page contains a reference to the source page. (If the page is not the target of a cross-page posting, or if the pages are in different applications, the PreviousPage() property is not initialized.) By default, the PreviousPage() property is typed as Page.


Here is the C# syntax to get control values of previous page


if (Page.PreviousPage != null)
{
TextBox SourceTextBox =
(TextBox)Page.PreviousPage.FindControl("TextBox1");
if (SourceTextBox != null)
{
Label1.Text = SourceTextBox.Text;
}
}



Register or Login to Post Your Opinion/Answer