Blue Theme Orange Theme Green Theme Red Theme
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Discover the top 5 tips for understanding .NET Interop
Search :       Advanced Search »
Home » Web Forms » Cross Page Postback in ASP.Net 2.0

Cross Page Postback in ASP.Net 2.0

In ASP.Net 2.0, cross-page post backs allow posting to a different web page, resulting in more intuitive, structured and maintainable code. In this article, we will explore the various options and settings for the cross page postback mechanism.

Author Rank :
Page Views : 134759
Downloads : 0
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
Nevron Chart
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

ASP.Net 1.1 provides for web forms posting back only to themselves. In many situations, the solution requires posting to a different web page. The traditional workaround alternatives were to use Response.Redirect and/or Server.Transfer to move to a different page and simulate cross page post-back behavior.

 

ASP.Net 2.0 provides a feature known as Cross Page PostBack for a web form to post-back to a different web form (other than itself)

 

How to post to a different page

 

To set a web form to post back to a different web form, in the source web form, set the PostBackURL property of a control that implements IButtonControl (eg. Button, ImageButton, LinkButton) to the target web form. When the user clicks on this button control, the web form is cross-posted to the target web form. No other settings or code is required in the source web form.

 

Access source page info within the posted page: FindControl Method

 

The target web form resulting from the cross-page postback provides a non-null PreviousPage property. This property represents the source page and provides reference to the source web form and its controls.

 

The controls on the source page can be accessed via the FindControl method on the object returned by the PreviousPage property of the target page.

 

protected void Page_Load(object sender, EventArgs e)

{

    ...

    TextBox txtStartDate = (TextBox) PreviousPage.FindControl("txtStartDate ");

    ...

}

 

At this point the target page does not have any knowledge of the source page. The PreviousPage property is of the type Page. For accessing controls using FindControl, the developer has to presume a certain structure in the source web form. This approach using FindControl has a few limitations. FindControl is dependent on the developer to provide the ids of the controls to access. The code will stop working if the control id is changed in the source web form. The FindControl method can retrieve controls only within the current container. If you need to access a control within another control, you need to first get a reference to the parent control.

 

Access source page info within the posted page: @PreviousPageType Directive

 

There is another more direct option to get access to the source page controls if the source page is pre-determined. The @PreviousPageType directive can be used in the target page to strongly type the source page. The directive specifies the source page using either the VirtualPath attribute or the TypeName attribute. The PreviousPage property then returns a strongly typed reference to the source page. It allows access to the public properties of the source page.

 

SourcePage.aspx:

 

<form runat="server" >
...
<asp:textbox runat="server" id
="txtFirstName"/>
<
asp:textbox runat="server" id
="txtLastName"/>
<
asp:button runat="server" id="btnViewReport" Text="View Report" PostbackURL="~/targetpage.aspx"
/>
...

public string FirstName

{

    get { return txtFirstName.Text; }

}

...

 

TargetPage.aspx

 

<%@ PreviousPageType VirtualPath="sourcepage.aspx" %>

string strFirstName;
strFirstName = PreviousPage.FirstName
//Strongly Typed PreviousPage allows direct access to the public properties of the source page.

Access source page info within the posted page: @Reference Directive

 

A third option to access the source page in a strongly typed fashion from the target page is to include an @Reference directive to the source page in the target page and then cast the PreviousPage property to the type of the source page.

 

Detect Cross Page PostBacks: IsCrossPagePostBack Property

 

When the source page cross-posts back to the target page, and the target page accesses the source page, the source page is reloaded in memory and goes through all the life cycle stages except the rendering. This version of the source page object is used by the target page to access information on the source page.

 

The IsCrossPagePostBack property in the source page indicates if it is being reloaded in memory in response to a PreviousPage reference from a target page.

  1. Page A cross postback to Page B
  2. Page B accesses the PreviousPage : Page A is reloaded in memory and the IsCrossPostBack property on this object has the value "true".

The IsCrossPagePostBack property can be used in the source page to prevent un-necessary processing from repeating when the page is being reloaded for the PreviousPage reference.

The PreviousPage.IsCrossPagePostBack property can be used to deduce if the current page has been loaded as a result of a cross page postback.

Validations

 

If the Source Page has any Validator controls, the source page will need to have valid input when it has cross posted to the target page. The target page can contain the validation check for PreviousPage.IsValid to catch invalid submissions to the source page.

 

Usage Scenarios

           

Previous limitations restricted the postback to the self page in ASP.Net 1.1. In ASP.Net 2.0, you can use cross-page post backs to post to a different web page, resulting in more intuitive, structured and maintainable code.

 

Example - A web form collects criteria for a web-based report (such as Report Type, Report Start Date and Report End Date) and the form is cross page posted to another web form which generates the report based on the parameters passed in the previous page.

 

A typical use for cross page postback is where you need a page to post to different pages by clicking on different button controls in the same form. The target page is specified with each button control, allowing postbacks to various forms.

 

When posting across applications, cross page postbacks allow access to the posted form data, but do not provide access to the source page's controls and viewstate.

 

References

 

Note:

This article is for purely educational purposes and is a compilation of notes, material and understanding on this subject. Any resemblance to other material is an un-intentional coincidence and should not be misconstrued as malicious, slanderous, or any anything else hereof.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Dipal Choksi
Dipal Choksi has over 10 years of industry experience in team-effort projects and also as an individual contributor. She has been working on the .Net platform since the beta releases of .Net 1.0.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Nevron Chart
Become a Sponsor
 Comments
Script info by srikanth On August 19, 2006

Hai

 

I am SrikanthReddy, I have 2 + of exp in .Net technologies.

my Main Problem is " When should i expose Scripting languages in .net Technologies

based on Performance wise and What is the Role of javaScript in .net technologies.

and which scripting languages is better supprot for .net technologies

"

I want to know this information

could you please send this details.

Thanking You

Bye.

Reply | Email | Modify 
Re: Script info by Dipal On August 22, 2006

Hi,

I assume you are referring to client-side scripting vs server side code. Each have their own usage and there are various opinions on their advantages/disadvantages.

In general, if you have a need to perform some operations which do not require a server round trip, client side script is suitable. An example would be displaying a confirmation message box to the user.

On the flip side, client side scripting could possibly lead to less security(which might be OK, depending in the specific situation). Also, you have to be careful regarding cross-browser support and situations where users have disabled client script on their browsers.

Hope this helps

 

Reply | Email | Modify 
Re: Re: Script info by srikanth On August 25, 2006

Hi,

Thanks for reply.

 I am using javascript at Passing parametrs one page to another page and

 validating of email text boxes and password textboxes.

                       and i have a question , At code behind i can maintain my data in cache  like cache("Connection")=con \\ connction object;

 and login information maintaing in sessions.

 using javascript can i do.if i write in javascript is it secured.

    if not secure    how can i write javascript code at CodeBehind. 

                             Thanking You.

                                                                   SrikanthReddy   

 

 

 

 

  

Reply | Email | Modify 
.net by megha On March 15, 2007
hi dipal i m a trainee developer i want to refresh page2 from page1 means without actuly clicking on the page2 i want to refresh it from page1 i think i want to creat page object but i cant access it plz help me by giving the code thanking u bye megha
Reply | Email | Modify 
Could you please help me? by Ahmad On October 28, 2010
I found in "Beginning ASP.Net 3.5 In C#" book a way to use the cross page postback by  casting  the PreviousPage property to the source page instead of putting it in the @PreviousPageType directive
And he wrote this code :
protected void Page_Load(object sender, EventArgs e)
{
if (PreviousPage != null)
{
lblInfo.Text = "You came from a page titled " +
PreviousPage.Title + "<br />";
CrossPage1 prevPage = PreviousPage as CrossPage1;
if (prevPage != null)
{
lblInfo.Text += "You typed in this: " + prevPage.FullName;
}
}
}
But when i try to use this code an error happened and says "The type or namespace name'CrossPage1'could not be found (are you missing a using directive or an assembly reference?)"

so what can i do to resolve this error sir?
Reply | Email | Modify 
how to retrieve documents in a database and display in a asp.net page by Ransford On April 2, 2011
i really need for my project which require the user to retrieve word or pdf document from a table and display its content on the brower in asp.net
Reply | Email | Modify 
DevExpress Free UI Controls
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.