What is Cross Page Posting in ASP.NET?

By default, buttons have a postback property. When you click the button it reloads the page itself. However we can use the property PostBackUrl to redirect to another page. If you want to use the data of one page to another page without using session, object, or anything else, you can just use cross-page in your project. 

Cross page posting means you are posting form data to another page. This is useful when you want to post data to another page and do not want incur the overhead of reloading the current page. The below code is given with a simple example. For this example we have to require two pages. Below is given the page with design and coding. Just follow along.

Cross-Page.aspx

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2.   
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4.   
  5. <head runat="server">  
  6.   
  7.     <title></title>  
  8.   
  9. </head>  
  10.   
  11. <body>  
  12.   
  13.     <form id="form1" runat="server">  
  14.   
  15.         <div align="center">  
  16.   
  17.             <asp:Label ID="Label1" runat="server" Text="User Name"></asp:Label>    
  18.             <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />  
  19.   
  20.             <asp:Label ID="Label2" runat="server" Text="Password"></asp:Label>      
  21.             <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>  
  22.   
  23.             <br />  
  24.   
  25.             <asp:Button ID="Button1" runat="server" Text="Log In" PostBackUrl="~/Cross_page2.aspx" />  
  26.   
  27.         </div>  
  28.   
  29.     </form>  
  30.   
  31. </body>  
  32.   
  33. </html>  

Cross-Pgae2.aspx

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2.   
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4.   
  5. <head runat="server">  
  6.   
  7.     <title></title>  
  8.   
  9. </head>  
  10.   
  11. <body>  
  12.   
  13.     <form id="form1" runat="server">  
  14.   
  15.         <div align="center">  
  16.   
  17.             <b style="font-size: xx-large; color: #669900"> Welcome To AspMaterials Blog</b><br />  
  18.   
  19.             <asp:Label ID="Label1" runat="server" Text="Label" Font-Bold="True" Font-Size="Large" ForeColor="Blue"></asp:Label>  
  20.   
  21.         </div>  
  22.   
  23.     </form>  
  24.   
  25. </body>  
  26.   
  27. </html>  

Cross-Page2.aspx.cs

  1. using System;  
  2.   
  3. using System.Collections.Generic;  
  4.   
  5. using System.Linq;  
  6.   
  7. using System.Web;  
  8.   
  9. using System.Web.UI;  
  10.   
  11. using System.Web.UI.WebControls;  
  12.   
  13. public partial class Cross_page2: System.Web.UI.Page  
  14.   
  15. {  
  16.   
  17.     protected void Page_Load(object sender, EventArgs e)  
  18.   
  19.     {  
  20.   
  21.         if (PreviousPage != null)  
  22.   
  23.         {  
  24.   
  25.             TextBox tb = new TextBox(); // this is a object for texbox  
  26.   
  27.             tb = (TextBox)(PreviousPage.FindControl("TextBox1"));  
  28.   
  29.             Label1.Text = tb.Text;  
  30.   
  31.         }  
  32.   
  33.     }  
  34.   
  35. }