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.
- <table>
- <tr>
- <td>Name:</td>
- <td>
- <asp:TextBox ID="txtName" runat="server" />
- </td>
- </tr>
- <tr>
- <td colspan="2">
- <asp:Button ID="BtnSubmit" Text="Move to next Page" runat="server" PostBackUrl="~/Display.aspx" />
- </td>
- </tr>
- </table>
In the second web form “Display.aspx” write the following HTML.
- <div>
- <asp:Label ID="LblName" runat="server"></asp:Label>
- </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”.
- protected void Page_Load(object sender, EventArgs e)
- {
- if (PreviousPage != null && PreviousPage.IsCrossPagePostBack)
- {
- TextBox txtName = (TextBox) PreviousPage.FindControl("txtName");
- LblName.Text = "Welcome " + txtName.Text;
- }
- else
- {
- Response.Redirect("Index.aspx");
- }
- }
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.

NTDP MurthyPosted Jun 18, 2019, 7:04 AM
It is not working. I put a debug in pageload. It is not going to if (PreviousPage)Condition in "Display.aspx.cs”.
Yogesh WaniPosted Oct 15, 2017, 2:46 PM
What if there is master page and the content is taken from page1.aspx to page2.aspx how it is possible?
anvesh dubeyPosted Sep 15, 2017, 4:22 PM
Good Explanation - Nice One
Santhakumar MunuswamyPosted Apr 13, 2015, 3:41 PM
Good work
Tom MohanPosted Mar 23, 2015, 8:20 PM
Good one.
Vithal WadjePosted Mar 23, 2015, 1:39 PM
nice one,Data get stored server memory
Gaurav GuptaPosted Mar 23, 2015, 5:02 AM
I think, In this case there no extra round trip to client to access the next page..
Sahil SharmaPosted Mar 23, 2015, 2:22 AM
Nice article nitin....just a query as using this technique, Is the data stored on client side or server side?