How to do Cross-Page Posting in Asp.Net and C#

Category > ASP.NET || Published on : Monday, August 25, 2014 || Views: 8033 || Cross-Page Posting in Asp.Net


Introduction:

Here Pawan Kumar has explained, How to do Cross-Page Posting in Asp.Net and C#

Description:-
In previous post I explained How to use UpdateProgress control in Asp.Net and C Sharp, How to play YouTube videos in ASP.NET Web Application, How to develop a Simple Chat Application in C# Using SignalR, How to use AJAX Timer Control in ASP.NET using C# with Example and Source Codes, How can we Insert, Update, Delete Records in ASP.NET Server Control ListView in ASP.NET with C#, How to Convert Numbers to Words String in ASP.NET using C#, Set meta tag in asp.net programmaticaly for creating seo friendly websites

We can post data from one page to another page to do we have to use cross-posting, you simply set "PostBackUrl" property to the name of another web form where we want to collect the particular data. When the user clicks the button, the page will be posted to that new URL with the values from all the input controls on the current page.

for this we have to create two pages CrossPage1.aspx and CrossPage2.aspx. The page named "CrossPage1.aspx" will have a form with two text boxes and a button. When the button is clicked, it posts to a page named CrossPage2.aspx.

HTML Codes for "CrossPage1.aspx" will be like below

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CrossPage1.aspx.cs" Inherits="CrossPage1" %>

"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

"http://www.w3.org/1999/xhtml">"server">

"form1" runat="server">
"txtFirstName" runat="server"> "txtLastName" runat="server"> "cmdSubmit" runat="server" Text="cross-page post" PostBackUrl="CrossPage2.aspx" />

and Write below code into CrossPage1 code behind for CrossPage1.aspx with this property:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class CrossPage1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    public string getFullName
    {
        get { return txtFirstName.Text + " " + txtLastName.Text; }
    }
}

and thats all with "CrossPage1.aspx"

Next we have to add another Webform named "CrossPage2.aspx" into the solution

then Add the "PreviousPageType" directive to the "CrossPage2.aspx" page

<%@ PreviousPageType VirtualPath="CrossPage1.aspx" %>

Complete ASPX code for the CrossPage2.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CrossPage2.aspx.cs" Inherits="CrossPage2" %>

<%@ PreviousPageType VirtualPath="CrossPage1.aspx" %>

"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

"http://www.w3.org/1999/xhtml">"server">

"form1" runat="server">
"lbltitle" runat="server" Text="Label">"lblInfo" runat="server" Text="Label">

Write the code in Code Behind of the "CrossPage2.aspx" to display the information or data passed from "CrossPage1.aspx"

protected void Page_Load(object sender, EventArgs e)
    {
        if (PreviousPage != null)
        {
            crossPage1 prevPage = PreviousPage as crossPage1;
            if (prevPage != null)
            {
              lblInfo.Text = "You Enter the name: " + prevPage.getFullName ;
            }
        }
    }

output will be like below.

so guys, we have learned in this tutorial, How to do Cross-Page Posting in Asp.Net and C#

 

Download Source Codes