Cross Page Posting PostBack In ASP.NET

In this example i am showing how to use Cross Page Posting or Postback In ASP.NET 2.0, 3.5, 4.0 Using C# And VB.NET. Cross Page posting is used to submit a form on one page (say default.aspx) and retrieve values of controls of this page on another page (say Default2.aspx)

There are two ways we can use cross page postsbacks in ASP.NET

1st method
In this i've created a Default.aspx page with two textbox and one button , button click will post back to Default2.aspx and there we will retrieve and show values of both textboxes

Html source of Default.aspx page is like
<%@ Page Language="C#" AutoEventWireup="true"  
CodeFile="Default.aspx.cs" Inherits="_Default" %>

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

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
First Name:
<asp:TextBox ID="txtFirstName" runat="server">
</asp:TextBox><br /><br />
Last Name:
<asp:TextBox ID="txtLastName" runat="server">
</asp:TextBox><br /><br /><br />
        
<asp:Button ID="btnSubmit" runat="server" 
            OnClick="btnSubmit_Click" 
            PostBackUrl="~/Default2.aspx"
            Text="Submit to Second Page" /><br />
</div>
</form>
</body>
</html>
Don't forget to set PostBackUrl Property of Button
PostBackUrl="~/Default2.aspx"

Now to retrieve values of textBoxes on Default2.aspx page, write below mentioned code in Page_Load event of second page (Default2.aspx)
C# code behind
protected void Page_Load(object sender, EventArgs e)
{
    //Check whether previous page is cross page post back or not
    if (PreviousPage != null && PreviousPage.IsCrossPagePostBack)
    {
        TextBox txtPbFirstName = (TextBox)PreviousPage.FindControl("txtFirstName");
        TextBox txtPbLastName = (TextBox)PreviousPage.FindControl("txtLastName");
        Label1.Text = "Welcome " + txtPbFirstName.Text + " " + txtPbLastName.Text;
    }
    else
    {
        Response.Redirect("Default.aspx");
    }
}
VB.NET Code behind
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    'Check whether previous page is cross page post back or not
    If PreviousPage IsNot Nothing AndAlso PreviousPage.IsCrossPagePostBack Then
        Dim txtPbFirstName As TextBox = DirectCast(PreviousPage.FindControl("txtFirstName"), TextBox)
        Dim txtPbLastName As TextBox = DirectCast(PreviousPage.FindControl("txtLastName"), TextBox)
        Label1.Text = ("Welcome " & txtPbFirstName.Text & " ") + txtPbLastName.Text
    Else
        Response.Redirect("Default.aspx")
    End If
End Sub

If you are using masterpages then you need to write code to FindControl as mentioned below
ContentPlaceHolder exampleHolder =(ContentPlaceHolder)Page.PreviousPage.Form.FindControl ("Content1"));
TextBox txtExample = exampleHolder.FindControl("txtFirstName");

2nd Method
Using Property to expose and Consume values of TextBox
If we are using this method then we don't need to use FindControl method at all
For this we need to create property in code behind of the page to be cross page post back (Default.aspx)
Html of the page needs no changes ,
C# code behind for Default.aspx
public TextBox pbTxtFirstName
    {
        get
        {
            return txtFirstName;
        }
    }

    public TextBox pbTxtLastName
    {
        get
        {
            return txtLastName;
        }
    }

VB.NET
Public ReadOnly Property pbTxtFirstName() As TextBox
    Get
        Return txtFirstName
    End Get
End Property

Public ReadOnly Property pbTxtLastName() As TextBox
    Get
        Return txtLastName
    End Get
End Property
Now to retrieve or consume exposed properties on Second page we need to add below mentioned page directive in html source of Default2.aspx page(usually at the top of page)
<%@ PreviousPageType VirtualPath="~/Default.aspx" %>
Now write this code in page_Load event of second page to retrieve values of controls
C# code
protected void Page_Load(object sender, EventArgs e)
{
    if (PreviousPage != null && PreviousPage.IsCrossPagePostBack)
    {
        Label1.Text = "Welcome " + PreviousPage.pbTxtFirstName.Text + " " + PreviousPage.pbTxtLastName.Text;
    }
    else
    {
        Response.Redirect("Default.aspx");
    }
}
VB Code
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    If PreviousPage IsNot Nothing AndAlso PreviousPage.IsCrossPagePostBack Then
        Label1.Text = ("Welcome " & PreviousPage.pbTxtFirstName.Text & " ") + PreviousPage.pbTxtLastName.Text
    Else
        Response.Redirect("Default.aspx")
        
    End If
End Sub

Hope this helps

29 comments:

  1. I love c# because is too simple. hahahaha

    ReplyDelete
  2. dear sir,
    Getting error to following code.
    error is OBJECT REFERENCE IS NOT SET TO ANINSTANCE OF AN OBJECT

    if (PreviousPage != null && PreviousPage.IsCrossPagePostBack)
    {
    ContentPlaceHolder exampleHolder = ((ContentPlaceHolder)Page.PreviousPage.Form.FindControl("Content123"));

    TextBox txtExample = (TextBox)exampleHolder.FindControl("txtFirstName");
    TextBox txtExample1 = (TextBox)exampleHolder.FindControl("txtLastName");
    Label1.Text = "Welcome " + txtExample.Text + " " + txtExample1.Text + "";
    }
    }
    }

    KINDLY HELP ME AS SOON AS POSSIBLE

    ReplyDelete
  3. by calling data via IsCrossPagePostBack from one page how to bind data on gridview which is located to another page

    ReplyDelete
  4. Wow of all the articles about crosspage posting and master pages, this is the best. Very well written. The first approach did not work, but the second one worked very well

    ReplyDelete
  5. Getting error to following code.
    error is OBJECT REFERENCE IS NOT SET TO ANINSTANCE OF AN OBJECT

    ReplyDelete
  6. Hello.
    The interesting name of a site - csharpdotnetfreak.blogspot.com, interesting this here is very good.
    I spent 5 hours searching in the network, until find your forum!

    ReplyDelete
  7. Hi - I am certainly glad to find this. cool job!

    ReplyDelete
  8. Hi - I am really glad to find this. Good job!

    ReplyDelete
  9. This comment has been removed by a blog administrator.

    ReplyDelete
  10. This comment has been removed by a blog administrator.

    ReplyDelete
  11. You made a few fine points there. I did a search on the subject matter and found mainly folks will consent with your blog.

    ReplyDelete
  12. Hello. And Bye.

    ReplyDelete
  13. Hi everybody! I am from Denmark and would like to say hello!

    ReplyDelete
  14. Everything is ok. But tell me what I need to do when I want to set e.g. Session Value on OnClick event? PostBackUrl fires first and omit OnClick event... How to resolve this and still having possibility to use PreviousPage?

    ReplyDelete
  15. hi,

    how to update data from one page to another without invoking that page. for example I click a button and the action to be reflected in another page without invoking it. it should be visible to any user who is currently using the other page.

    ReplyDelete
  16. good one for cross page post back

    ReplyDelete
  17. I consider, that you are not right. I am assured. I can prove it. Write to me in PM.

    ReplyDelete
  18. This comment has been removed by a blog administrator.

    ReplyDelete
  19. This comment has been removed by a blog administrator.

    ReplyDelete
  20. This comment has been removed by a blog administrator.

    ReplyDelete
  21. Good day!
    Happy New Year!
    Health, luck and love!

    ReplyDelete
  22. This comment has been removed by a blog administrator.

    ReplyDelete
  23. hi sir i ma getting NullReferenceExeption at below line

    V= Label1.Text = "Welcome " + txtPbFirstName.Text + " " + txtPbLastName.Text;

    Plz help me

    ReplyDelete
  24. @Above: Use PreviousPage.pbTxtFirstName.Text

    ReplyDelete
  25. Your site is really good and the posts are just wonderful. Thank you and keep doing your great work.

    ReplyDelete
  26. You could have done this

    public string pbTxtFirstName
    { get {return txtName.Text;}}

    Instead of getting whole TextBox

    ReplyDelete
  27. Thanks for sharing this information.

    ReplyDelete
  28. help me on use of repeater controle

    ReplyDelete
  29. Beginner C#, jQuery, SQL Server, ASP.NET MVC, LINQ and much more..

    Beginner Programming Tutorials

    ReplyDelete