What Is Cross Page Posting In ASP.NET Web Form

What is Cross Page Posting?

It means you are posting form data from one page to another page.

By default Asp.net web form page ,button and other controls that cause post back submits current page back to itself.

Some Cross page posting techniques,

Using PostBackUrl property

Suppose you are on Default.aspx and want to display textbox data from Default.aspx to a label present on Display.aspx.

Code on Default.aspx

  1. <form id="form1" runat="server">  
  2.    <asp:TextBox ID="txtName" name="data" runat="server" />  
  3.    <asp:Button Text="ClickPostBack" runat="server" PostBackUrl="~/Display.aspx" />  
  4. </form>  

Code on Display.aspx

  1. <form id="form1" runat="server">  
  2.    <asp:Label Text="text" name="lblValue" ID="lblValue" runat="server" />  
  3. </form>  

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.         lblValue.Text = "Welcome " + txtName.Text;  
  7.     }  
  8. }  

Output
Output

Note

  1. What is IsCrossPagePostBack property? It gets a value indicating whether the page is involved in a cross-page postback.True if its an cross post back request.

  2. Its an client side technique.

Server.Transfer with Query String

Code on Default.aspx

  1. <form id="form1" runat="server">  
  2.    <asp:Button ID="btnclick" runat="server" Text="ClickMeUsingServerTransfer" onclick="btnclick_Click" />  
  3. </form>  

Code on button click event on Default.aspx.cs

  1. protected void btnclick_Click(object sender, EventArgs e)  
  2. {  
  3.    Server.Transfer("Display.aspx?Name=Vikas&Work=Developer");  
  4. }  

Code on button click event on Display.aspx.cs

  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3.    if (PreviousPage != null)  
  4.    {  
  5.       lblValue.Text = "Welcome" + Request.QueryString["Name"] + ": .Net " + Request.QueryString["Work"];  
  6.    }  
  7. }  
Output

Output

Response.Redirect with Query String

Cross page posting won’t work here as it does not preserve query string and form variables.