In this article we will see how to use 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 that comes in a developer’s mind are the sessions. But using session can be bad sometimes as the page gets heavy because of this. There is one more way to achieve this and thereby avoiding 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 for the same.
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 in the “Index.aspx” web form.
When there is a need to transfer some data from one web page to another web page then normally the first thing that comes in a developer’s mind are the sessions. But using session can be bad sometimes as the page gets heavy because of this. There is one more way to achieve this and thereby avoiding 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 for the same.
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 in 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 name. When
we click the button the textbox value should get transferred to the
Display page. To achieve 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 that there exists a previous page from where
the request is coming. In other words it is used to prevent a user
directly opening this page. If the user does this then the condition
returns false.
The second condition checks whether really a cross page postback has 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 next page. Refer below image.

As expected we have the Name transferred from Index page to the Display page. So without using sessions we have achieved this scenario.
Now we will try to browse the “Display.aspx” page directly just to make sure 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 as per the condition defined in the else condition in the page load
The second condition checks whether really a cross page postback has 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 next page. Refer below image.

As expected we have the Name transferred from Index page to the Display page. So without using sessions we have achieved this scenario.
Now we will try to browse the “Display.aspx” page directly just to make sure 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 as per the condition defined in the else condition in the page load
No comments:
Post a Comment