Cross Page Posting In ASP.NET 2.0
Posted by: Suprotim Agarwal ,
on 8/5/2007,
in
Category ASP.NET
Abstract: Cross-page posting is desired in a scenario where data is collected on one Web page and processed on another Web page. ASP.NET 2.0 introduces a new property in the Page class called ‘PreviousPage’ which gets the page that posted to the current page . In this article, we will see how to use Cross Page Posting in ASP.NET 2.0. I assume that you have some experience in creating ASP.NET pages.
Cross Page Posting In ASP.NET
ASP.NET by default, submits the form to the same page. Cross page posting is submitting the form to a different page. This is usually required when you are creating a multi page form to collect information from the user on each page. When moving from the source to the target page, the values of controls in the source page can be accessed in the target page.
To use cross-page posting, you have to use the PostBackURL attribute to specify the page you want to post to.
Follow these steps :
Step 1: Create a new ASP.NET website called CrossPagePosting. By default, the website is created with a single webpage, Default.aspx. Right click the project in the Solution Explorer > Add New Item >Web Form. Keep the original name Default2.aspx and click ‘Add’. The website will now contain two pages, Default.aspx and Default2.aspx.
Step 2: On the source page, Default.aspx, drop a button on the form. Set the text of the button as ‘TargetButton’. Set the ‘PostBackUrl’ property of a Button to the URL of the target page, Default2.aspx.
<asp:Button ID="Button1" runat="server" PostBackUrl="~/Default2.aspx" Text="TargetButton" /></div>
Step 3: In the target page Default2.aspx, drop a label on the page from the toolbox.
Step 4: In the Page_Load() of Default2.aspx, you can then access the ‘PreviousPage’ property to check if the page is being accessed as Cross Page postback.
protected void Page_Load(object sender, EventArgs e)
{
if (Page.PreviousPage != null)
{
}
}
Step 5: To retrieve values from the source page, you must access controls using the ‘FindControl()’ method of the ‘PreviousPage’. We will be accessing the Text property of the Button control placed in Default.aspx.
protected void Page_Load(object sender, EventArgs e)
{
if (Page.PreviousPage != null)
{
Button btn = (Button)(Page.PreviousPage.FindControl("button1"));
Label1.Text = btn.Text;
}
}
Step 6: In the Solution Explorer, right click Default.aspx > ‘Set as Start Page’. Run the application and click on the button. As you can observe, the page is posted to Default2.aspx and the value containing the name of the button control gets displayed in the label.
Difference between Server.Transfer and Cross Page Posting
One could argue that even the ‘Server.Transfer’ method can be used to move between pages. However there are a few differences between ‘Server.Transfer’ and ‘Cross Page’ Posting.
In ‘Server.Transfer’, the URL doesn't change whereas in cross page posting, the form is submitted to a different page, thereby changing the url.
The Server.Transfer method is a server-based operation whereas cross-page postback is a client-based transfer.
Note: To determine whether the page was invoked from a cross-page posting or a Server.Transfer operation, the Page class exposes a property named IsCrossPagePostBack.
Tips and Tricks
Tip 1: If you are using a MasterPage for all your pages, you can access the control using the following code.
Assuming that the content id and placeholder id is named ‘Content1’.
ContentPlaceHolder pageContent =
(ContentPlaceHolder)
(Page.PreviousPage.Form.FindControl ("Content1"));
TextBox1.Text = pageContent.FindControl("button1");
Tip 2: To avoid casting the control type, you can also access the previous page data(Default1.aspx) by creating public properties that expose the data that you need to access.
In Default1.aspx
public String BTarget
{
get
{
return button1.Text;
}
}
In the ‘Default2.aspx’, access the value of the Button using the exposed property
Label1.Text = PreviousPage.BTarget;
Conclusion
In this article, we saw that ASP.NET 2.0 introduces new features whereby pages can post to pages other than themselves. We also examined the differences between Server.Transfer and Cross Page Posting. I hope this article was useful and I thank you for viewing it.
This article has been editorially reviewed by Suprotim Agarwal.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.
Click here to Explore the Table of Contents or Download Sample Chapters!
Was this article worth reading? Share it with fellow developers too. Thanks!
Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of
DotNetCurry,
DNC Magazine for Developers,
SQLServerCurry and
DevCurry. He has also authored a couple of books
51 Recipes using jQuery with ASP.NET Controls and
The Absolutely Awesome jQuery CookBook.
Suprotim received the prestigious Microsoft MVP award for 17 consecutive years, until he resigned from the program in 2025. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that offers Digital Marketing and Branding services to businesses, both in a start-up and enterprise environment.
Get in touch with him on Twitter @suprotimagarwal or at LinkedIn