Cross Page Posting or Postback to Transfer Data in Asp.net C# VB.Net

In Asp.net 1.1 we can postback a form itself but can’t posting back to other pages. This is a limitation of Asp.net 1.0. But from upper versions we can simulate cross page postback by using Response.Redirect or Server.Transfer that i have already described in one of my previous post named “Passing data/parameters/values from one aspx page to another aspx page“.

In Asp.net 2.0 or upper versions Microsoft gives us a solution to easily do the cross page postback that I will cover in this article. The feature known as “Cross Page PostBack” from a web form or aspx page to post back to a different or another web form or aspx page.

Asp.net 2.0 or upper versions offers us to post back to another form by using PostBackURL property of a control that implements IButtonControl. Here note that Button, ImageButton, LinkButton has the PostBackURL property to submit one page/form to different page/form. Also provides us a FindControl Method to catch the controls of previous page or source page. By using PreviousPage property we will catch the page that has been submitted. So using PreviousPage property we can catch the previous page and using FindControl Method we can catch the controls of the previous page.

There are two ways we can post back a form into another form. These are:
1. Direct access controls from target page
2. Direct access public properties from target page

Direct access controls from target page:
To describe this example first create a project. Here we will generate cross page postback from Default.aspx to About.aspx page. Now add two TextBox in source page means Default.aspx and a Button control. Set the PostBackUrl=”About.aspx” of the Button control. No server side code is required for the Default.aspx since after clicking on the Default page Button Control, the page will submit itsself into the About.aspx page that i have specified in PostBackUrl property. Have a look at source page Default.aspx markup:

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:Label ID="lblFirstNum" runat="server">Enter First Number:</asp:Label><br />
    <asp:TextBox ID="txtFirstNum" runat="server"></asp:TextBox><br />
    
    <br />
    <br />

    <asp:Label ID="lbl2ndNum" runat="server">Enter 2nd Number: </asp:Label><br />
    <asp:TextBox ID="txt2ndNum" runat="server"></asp:TextBox><br/>

     <br />
    <br />
    
    <asp:Button ID="cmdCross" runat="server" Text="Click to Trigger Postback" PostBackUrl="About.aspx" />
</asp:Content>

Now we have to do some work on About.aspx to collect data from Default.aspx page. By using PreviousPage property & FindControl Method we can easily catch data from source page. Have a look at the About.aspx page_load event where we catch source page first number & Second number & show the cumulative result in About.aspx page. Code behind of About.aspx Page_Load event:

C# Code:

            if (PreviousPage != null && PreviousPage.IsCrossPagePostBack)
            {

                ContentPlaceHolder content = (ContentPlaceHolder)Page.PreviousPage.Form.FindControl("MainContent");

                decimal nFirstNum = Convert.ToDecimal(((TextBox)content.FindControl("txtFirstNum")).Text);

                decimal n2ndNum = Convert.ToDecimal(((TextBox)content.FindControl("txt2ndNum")).Text);

                Response.Write("Sum of previous page two number is: " + (nFirstNum + n2ndNum));

            }

            else
            {

                // This is not cross page postback.

                // Implement Other logic here

            }

VB.Net Code:

            If PreviousPage IsNot Nothing AndAlso PreviousPage.IsCrossPagePostBack Then

                Dim content As ContentPlaceHolder = DirectCast(Page.PreviousPage.Form.FindControl("MainContent"), ContentPlaceHolder)

                Dim nFirstNum As Decimal = Convert.ToDecimal(DirectCast(content.FindControl("txtFirstNum"), TextBox).Text)

                Dim n2ndNum As Decimal = Convert.ToDecimal(DirectCast(content.FindControl("txt2ndNum"), TextBox).Text)


                Response.Write("Sum of previous page two number is: " & (nFirstNum + n2ndNum))
            Else

                ' This is not cross page postback.

                ' Implement Other logic here
            End If

If you are not using Master Page. Then follow the below code:
C# Code:

if (PreviousPage != null && PreviousPage.IsCrossPagePostBack)

            {

                decimal nFirstNum = Convert.ToDecimal(((TextBox)PreviousPage.FindControl("txtFirstNum")).Text);

                decimal n2ndNum = Convert.ToDecimal(((TextBox)PreviousPage.FindControl("txt2ndNum")).Text);

                Response.Write("Sum of previous page two number is: " + (nFirstNum + n2ndNum));

            }

VB.Net Code:

If PreviousPage IsNot Nothing AndAlso PreviousPage.IsCrossPagePostBack Then


	Dim nFirstNum As Decimal = Convert.ToDecimal(DirectCast(PreviousPage.FindControl("txtFirstNum"), TextBox).Text)

	Dim n2ndNum As Decimal = Convert.ToDecimal(DirectCast(PreviousPage.FindControl("txt2ndNum"), TextBox).Text)


	Response.Write("Sum of previous page two number is: " & (nFirstNum + n2ndNum))
End If

Direct access public properties from target page:
To direct access public properties you have to create some public properties in source page which we will access from target page. To do that modify Default.aspx page server side code in the following way & keep HTML mark up as it is:

C# Code:

    public decimal nFirstNum
    {
        get { return Convert.ToDecimal(txtFirstNum.Text); }
    }

    public decimal n2ndNum
    {
        get { return Convert.ToDecimal(txt2ndNum.Text); }
    }

VB.Net Code:

    Public ReadOnly Property nFirstNum() As Decimal
        Get
            Return Convert.ToDecimal(txtFirstNum.Text)
        End Get
    End Property

    Public ReadOnly Property n2ndNum() As Decimal
        Get
            Return Convert.ToDecimal(txt2ndNum.Text)
        End Get
    End Property

Don’t forget to add the below page directives into the About.aspx. Otherwise the next code segment will give you error since in page_load event you need to access Default page public properties. The directive is:

 <%@ PreviousPageType VirtualPath="~/Default.aspx" %>

Now also modify About.aspx page_load event in the following way:
C# Code:

            if (PreviousPage != null && PreviousPage.IsCrossPagePostBack)
            {
                decimal nFirstNum = PreviousPage.nFirstNum;
                decimal n2ndNum = PreviousPage.n2ndNum;
                Response.Write("Sum of previous page two number is: " + (nFirstNum + n2ndNum));
            }
            else
            {
                // This is not cross page postback.
                // Implement Other logic here
            }
[/sourceode]

<strong>VB.Net Code:</strong>

            If PreviousPage IsNot Nothing AndAlso PreviousPage.IsCrossPagePostBack Then
                Dim nFirstNum As Decimal = PreviousPage.nFirstNum
                Dim n2ndNum As Decimal = PreviousPage.n2ndNum
                Response.Write("Sum of previous page two number is: " & (nFirstNum + n2ndNum))
            Else
                ' This is not cross page postback.

                ' Implement Other logic here
            End If

The Output will be like this:
Cross Page Posting or Postback in Asp.net CSharp VB.Net

Download Code Example C#        Download Code Example VB.Net

Leave a Reply

Your email address will not be published. Required fields are marked *

AlphaOmega Captcha Classica  –  Enter Security Code