Introduction
Here I will explain what is cross page posting in asp.net or Cross Page Postback in asp.net
using C# and VB.NET.
Description
In previous articles I explained Send values from one page to another page using QueryString,
Joins in SQL Server, Highlight Gridview records based on search and many
articles relating to Gridview,
SQL, jQuery, asp.net,
C#, VB.NET.
Now I will explain what is cross page postback in asp.net and how to use cross
page postback concept in asp.net.
Cross
Page Postback
Cross page postback is the concept which is used to
submit one page (Default.aspx) controls to another page (Default2.aspx) and
access those page (Default.aspx) control values in another page (Default2.aspx).
Here I will explain this concept with simple example
for that first create one web application and add two new pages Default.aspx and Default2.aspx.
Now open Default.aspx
page and write the following code
| 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
<title>Cross Page Postback
  Example in asp.net</title> 
</head> 
<body> 
<form id="form1"
  runat="server"> 
<div> 
<table> 
<tr> 
<td><b>Enter UserName:</b></td> 
<td><asp:TextBox ID="txtUserName"
  runat="server"/></td> 
</tr> 
<tr> 
<td><b>Enter Location:</b></td> 
<td><asp:TextBox ID="txtLocation"
  runat="server"/></td> 
</tr> 
<tr> 
<td></td> 
<td><asp:Button ID="btnPostback"
  Text="Postback"
  runat="server"
  PostBackUrl="~/Default2.aspx"
  /> </td> 
</tr> 
</table> 
</div> 
</form> 
</body> 
</html> | 
If you observe button control code I added property PostBackUrl="~/Default2.aspx" by using this property we will submit Default.aspx page
control values to Default2.aspx page.
Now open Default2.aspx
page and write the following code 
| 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
<title>Cross Page Postback
  Example in asp.net</title> 
</head> 
<body> 
<form id="form1"
  runat="server"> 
<div> 
<b><u>Default2.aspx Page</u></b><br /><br /> 
<label id="lblName"
  runat="server"
  /><br
  /><br
  /> 
<label id="lblLocation"
  runat="server"
  /> 
</div> 
</form> 
</body> 
</html> | 
Now open Default2.aspx
code behind file and add following namespaces
C#
Code
| 
using System; 
using
  System.Web.UI.WebControls; | 
Once namespaces added write the following code 
| 
protected void Page_Load(object
  sender, EventArgs e) 
{ 
if (PreviousPage != null
  && PreviousPage.IsCrossPagePostBack) 
{ 
TextBox txtName =
  (TextBox)PreviousPage.FindControl("txtUserName"); 
TextBox
  txtLocation = (TextBox)PreviousPage.FindControl("txtLocation"); 
lblName.InnerText = "Welcome to
  Default2.aspx page " + txtName.Text; 
lblLocation.InnerText = "Your
  Location: " + txtLocation.Text; 
} 
else 
{ 
Response.Redirect("Default.aspx"); 
} 
} | 
VB.NET
Code
| 
Imports
  System.Web.UI.WebControls 
Partial Class Default3 
Inherits System.Web.UI.Page 
Protected Sub Page_Load(sender As
  Object, e As EventArgs) Handles
  Me.Load 
If PreviousPage IsNot
  Nothing AndAlso
  PreviousPage.IsCrossPagePostBack Then 
Dim txtName As TextBox = DirectCast(PreviousPage.FindControl("txtUserName"), TextBox) 
Dim txtLocation As TextBox = DirectCast(PreviousPage.FindControl("txtLocation"), TextBox) 
lblName.InnerText = "Welcome to
  Default2.aspx page " & txtName.Text 
lblLocation.InnerText = "Your
  Location: " & txtLocation.Text 
Else 
Response.Redirect("Default.aspx") 
End If 
End Sub 
End Class | 
If you observe above codePreviousPageproperty
of current page (Default2.aspx)is NOT NULL, this is to avoid errors in
case that user opens our Default2.aspx directly, and here we checked another
property called IsCrossPagePostBack to see if we really had a
CrossPagePostback.
If
both properties Page.PreviousPage is NOT NULL and PreviousPage.IsCrossPagePostback
is true then only we can say
CrossPagePostback happen.
Demo
Download
Sample Code Attached
| If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. | |||
|  Subscribe by RSS  Subscribe by Email | |||


 
8 comments :
it's really nice article
i think this is more easier like,
in default2.aspx, in page_load
assign
string uname,loc;
uname=Request.form["txtUserName"];
loc=Request.form["txtLocation"];
lblName.text=string.format("Welcome to Default2.aspx page {0}
Your Location: {1}",uname,loc)
only 1 label is enough
SIR PLEASE TELL ME LIKE GMAIL SUPPOSE I AM LOGGING IN GMAIL THEN THERE IS ALSO A CROSS PAGE POSTBACK WITH SOME GIF IMAGE WHICH SHOW THE WATING ,SIR IS THIS POSSIBLE WITH THIS CODING/PLEASE ANSWER ME SIR
Valuable example it's a great article thanks for sharing this informative information.. I will visit your blog regularly for some latest post.
Awesome...!
gud
Sir, I am NOT getting Innerttext property for lable....
I have a treeview in one page and using navigateurl, I display another page within an Iframe in the first page. While closing the child page, I want to Deselect the Treeview treenode which is highlighted [SelectedNode Style]. How to do this?
Note: Only a member of this blog may post a comment.