in Education by
What is cross-page posting in ASP.NET?

1 Answer

0 votes
by

 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. 

  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3.    ...  
  4.    TextBox txtStartDate = (TextBox) PreviousPage.FindControl("txtStartDate ");  
  5.    ...  
  6. }  

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

  1. <form runat="server"> ...  
  2.     <asp:textbox runat="server" id="txtFirstName" />  
  3.     <asp:textbox runat="server" id="txtLastName" />  
  4.     <asp:button runat="server" id="btnViewReport" Text="View Report" PostbackURL="~/targetpage.aspx" /> ... public string FirstName { get { return txtFirstName.Text; } } ...  

TargetPage.aspx

  1. <%@ PreviousPageType VirtualPath="sourcepage.aspx" %>  
  2. string strFirstName;  
  3. strFirstName = PreviousPage.FirstName //Strongly Typed PreviousPage allows direct acces

Related questions

0 votes
    How can we improve the Performance of an ASP.NET Web Page?...
asked Apr 8, 2021 in Education by JackTerrance
0 votes
0 votes
    What is the ASP.NET page life cycle events?...
asked Apr 7, 2021 in Education by JackTerrance
0 votes
0 votes
    What is master page in ASP.NET?...
asked Apr 6, 2021 in Education by JackTerrance
0 votes
    What is Enterprise Library in ASP.NET?...
asked Apr 8, 2021 in Education by JackTerrance
0 votes
    What is Data Cache in ASP.NET and how to use?...
asked Apr 8, 2021 in Education by JackTerrance
0 votes
0 votes
    What are the Navigations techniques in ASP.NET?...
asked Apr 8, 2021 in Education by JackTerrance
0 votes
    What is Themes in ASP.NET?...
asked Apr 8, 2021 in Education by JackTerrance
0 votes
    Explain Cookie-less Session in ASP.NET?...
asked Apr 8, 2021 in Education by JackTerrance
0 votes
    What is the PostBack property in ASP.NET?...
asked Apr 8, 2021 in Education by JackTerrance
0 votes
0 votes
    What are the differences between ASP.NET HttpHandler and HttpModule?...
asked Apr 7, 2021 in Education by JackTerrance
0 votes
    What is the difference between HttpContext.Current.Items and HttpContext.Current.Session in ASP.NET?...
asked Apr 7, 2021 in Education by JackTerrance
...