Cross-Page Postback in ASP.Net

When there is a need to transfer some data from one web page to another web page then normally the first thing a developer thinks of is sessions. But the use of a session can be bad sometimes since the page becomes heavy due to it. There is one more way to do it and thereby avoid sessions or other state management techniques, we can use a cross page postback. It simply transfers the data from one page to another. In this article we will create a sample application to check the same.

Let us see a sample demo of it.

Open Visual Studio, create a new web application. Name it “CrossPagePostbackDemo”.

Add a Web Form to the project. Name it “Index.aspx”. Add another web form to the project. Name it “Display.aspx”.

Add the following HTML code to the “Index.aspx” web form.

  1. <table>  
  2.     <tr>  
  3.         <td>Name:</td>  
  4.         <td>  
  5.             <asp:TextBox ID="txtName" runat="server" />  
  6.         </td>  
  7.     </tr>  
  8.     <tr>  
  9.         <td colspan="2">  
  10.             <asp:Button ID="BtnSubmit" Text="Move to next Page" runat="server" PostBackUrl="~/Display.aspx" />  
  11.         </td>  
  12.     </tr>  
  13. </table> 

In the second web form “Display.aspx” write the following HTML.

  1. <div>  
  2.     <asp:Label ID="LblName" runat="server"></asp:Label>  
  3. </div> 

So basically in our first form we have a TextBox that accepts a name. When we click the button the TextBox value should be transferred to the Display page. To do this we need to write a bit of code in the “Display.aspx.cs”.

Write the following code in the “Display.aspx.cs”.

  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3.     if (PreviousPage != null && PreviousPage.IsCrossPagePostBack)  
  4.     {  
  5.         TextBox txtName = (TextBox) PreviousPage.FindControl("txtName");  
  6.         LblName.Text = "Welcome " + txtName.Text;  
  7.     }  
  8.     else  
  9.     {  
  10.             Response.Redirect("Index.aspx");  
  11.     }   

As we can see in the page load of this file we have two conditions. The first condition checks whether a previous page exists from where the request is coming. In other words it is used to prevent a user from opening this page directly. If the user does this then the condition returns false. The second condition checks whether a cross-page postback has really occurred.

Set Index.aspx as the starting page in the application. Run the Page.

Enter some text and hit the button.



Once the button is clicked we are redirected to the next page. Refer to the following image.



As expected we have the Name transferred from the Index page to the Display page. So without using sessions we have accomplished this scenario.
Now we will try to browse the “Display.aspx” page directly just to ensure we do not get to see the same output. So just browse the second page directly and see the output.



We are redirected to the first page depending on the condition defined in the else condition in the page load.