Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Cross Page PostBack in ASP.NET

Author: Mike Green
Download Source Code : 1314_CrossPagePostBack.zip

By default, ASP.NET Page is PostBack to itself. However it can PostBack to another page. I will explain it with different ways in this article.

 

What is Cross Page PostBack?

When a page is posted back to a different page then it is called Cross Page PostBack. There are many situations when you need to post the content of one page to another. This can be accomplished by using Cross Page PostBack feature of ASP.NET. You can use a Button control on a page to post to different target page. Cross Page PostBack actually means that you are posting your form data to another page instead of posting to the same page.
 
ASP.NET provides different ways to do Cross Page PostBack. I will discuss these methods of Cross Page PostBack one by one.

First Method (Checking Previous Page.IsCrossPagePostBack Property)

  1. Open MS Visual Studio 2010
  2. File > New > Website > Visual C# or Visual Basic > ASP.NET Empty Web Site
  3. Select Web Location as File System and Click OK
  4. From Menu, Website > Add New Item > Select Web Form and Click Add. Name of the Web Form will be Default.aspx
  5. From Menu, Website > Add New Item > Select Web Form and Click Add. Rename the Web Form as TargetPage1.aspx
  6. Drag and Drop three Label Control,  three TextBox controls and one button or write code below in Default.aspx page

    <asp:Label ID="Label1" runat="server" Text="Name:"></asp:Label>
    <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
    <br />
    <asp:Label ID="Label2" runat="server" Text="Gender:"></asp:Label>
    <asp:TextBox ID="txtGender" runat="server"></asp:TextBox>
    <br />
    <asp:Label ID="Label3" runat="server" Text="City:"></asp:Label>
    <asp:TextBox ID="txtCity" runat="server"></asp:TextBox>
    <br />
    <asp:Button ID="btnFirst" runat="server" Text="First Method"
                PostBackUrl="~/TargetPage1.aspx" />

    You need to set the PostBackUrl attribute of the Button to the target page URL for PostBack. In our case it is “~/TagetPage1.aspx” 
     
  7. Open TargetPage1.aspx and Drag and Drop three Label controls or write code below in it.

    <asp:Label ID="lblName" runat="server" ></asp:Label>
    <br />
    <asp:Label ID="lblGender" runat="server" ></asp:Label>
    <br />
    <asp:Label ID="lblCity" runat="server" ></asp:Label>
     
  8. Open code behind file of TargetPage1.aspx.cs or TargetPage1.aspx.vb and write code below in Page Load Method.

    C#
     
    protected void Page_Load(object sender, EventArgs e)
    {
        if (this.PreviousPage.IsCrossPagePostBack)
        {
            TextBox name = (TextBox)this.PreviousPage.FindControl("txtName");
            TextBox gender = (TextBox)this.PreviousPage.FindControl("txtGender");
            TextBox city = (TextBox)this.PreviousPage.FindControl("txtCity");
     
            lblName.Text = name.Text;
            lblGender.Text = gender.Text;
            lblCity.Text = city.Text;
        }
    }
     
    VB.NET
     
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
       If Me.PreviousPage.IsCrossPagePostBack Then
            Dim name As TextBox = DirectCast(Me.PreviousPage.FindControl("txtName"), TextBox)
            Dim gender As TextBox = DirectCast(Me.PreviousPage.FindControl("txtGender"), TextBox)
            Dim city As TextBox = DirectCast(Me.PreviousPage.FindControl("txtCity"), TextBox)
     
            lblName.Text = name.Text
            lblGender.Text = gender.Text
            lblCity.Text = city.Text
        End If
    End Sub
     
    We need to check the PreviousPage.IsCrossPagePostBack property. Then we have to find TextBox Controls of the previous page to get the values. This can be done by using FindControl() Method of PreviousPage property. Then we can display and use values of the TextBox Controls from the previous page
     
  9. Now right click on Default.aspx page and Click on “Set As Start Page”. Browse the website, provide values in text boxes and click on button to see the result.


    Second Method (Using public Properties and PreviousPageType directive)
     
  10. From Menu, Website > Add New Item > Select Web Form and Click Add. Rename the Web Form as TargetPage1.aspx
  11. Open Default.aspx page and drag and drop another Button control.

    <asp:Button ID="btnSecond" runat="server" Text="Second Method"
                PostBackUrl="~/TargetPage2.aspx" />
     
    You need to set the PostBackUrl attribute of the Button to the target page URL for PostBack. In our case it is “~/TagetPage2.aspx”
     
  12. Open code behind file of Default.aspx.cs or Default.aspx.vb and write code below in it.

    C#
     
    public string Name
    {
        get
        {
            return txtName.Text;
        }
    }
    public string Gender
    {
        get
        {
            return txtGender.Text;
        }
    }
    public string City
    {
        get
        {
           return txtCity.Text;
        }
    }
     
    VB.NET
     
    Public ReadOnly Property Name() As String
        Get
            Return txtName.Text
        End Get
    End Property
    Public ReadOnly Property Gender() As String
        Get
            Return txtGender.Text
        End Get
    End Property
    Public ReadOnly Property City() As String
        Get
            Return txtCity.Text
        End Get
    End Property
     
    Here we have set the Public ReadOnly properties and return the text of each TexBox Controls.
     
  13.  Open TargetPage2.aspx page and drag and drop three Label controls to display retrieved values.

    <asp:Label ID="lblName" runat="server" ></asp:Label>
    <br />
    <asp:Label ID="lblGender" runat="server" ></asp:Label>
    <br />
    <asp:Label ID="lblCity" runat="server" ></asp:Label>

     
  14.  Add “PreviousPageType” directive after “Page” directive in TargetPage2.aspx

    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="TargetPage2.aspx.vb" Inherits="TargetPage2" %>
     
    <%@ PreviousPageType VirtualPath="~/Default.aspx" %>
     
    You need to set its VirtualPath attribute to Default.aspx page.
     
  15. Open TargetPage2.aspx.cs or TargetPage2.aspx.vb and write code below in Page Load method.

    C#
     
    protected void Page_Load(object sender, EventArgs e)
    {
        if (this.PreviousPage.IsCrossPagePostBack)
        {
            lblName.Text = PreviousPage.Name;
            lblGender.Text = PreviousPage.Gender;
            lblCity.Text = PreviousPage.City;
        }
     }
     
    VB.NET
     
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        If Me.PreviousPage.IsCrossPagePostBack Then
            lblName.Text = PreviousPage.Name
            lblGender.Text = PreviousPage.Gender
            lblCity.Text = PreviousPage.City
        End If
    End Sub
     
    Here we have to check the PreviousPage.IsCrossPagePostBack property and get the properties for our TextBox controls
     
  16. Browse the website, provide values in text boxes and click on Second Method button to see the result.

    Third Method (Server.Transfer())
     
  17. From Menu, Website > Add New Item > Select Web Form and Click Add. Rename the Web Form as TargetPage1.aspx
  18. Open Default.aspx page and drag and drop another Button control.

    <asp:Button ID="btnThird" runat="server" Text="Third Method"
                onclick="btnThird_Click" />
     
    Add the “onClick” attribute of the Button control.
     
  19. Open code behind file of Default.aspx.cs or Default.aspx.vb and write button click event in it.

    C#
     
    protected void btnThird_Click(object sender, EventArgs e)
    {
        Server.Transfer("TargetPage3.aspx");
    }
     
    VB.NET
     
    Protected Sub btnThird_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnThird.Click
        Server.Transfer("TargetPage3.aspx")
    End Sub

     
  20. Open TargetPage3.aspx page and drag and drop three Label controls to display retrieved values.

    <asp:Label ID="lblName" runat="server" ></asp:Label>
    <br />
    <asp:Label ID="lblGender" runat="server" ></asp:Label>
    <br />
    <asp:Label ID="lblCity" runat="server" ></asp:Label>
     
  21. Open TargetPage2.aspx.cs or TargetPage2.aspx.vb and write code below in Page Load method.

    C#
     
    protected void Page_Load(object sender, EventArgs e)
    {
        TextBox name = (TextBox)this.PreviousPage.FindControl("txtName");
        TextBox gender = (TextBox)this.PreviousPage.FindControl("txtGender");
        TextBox city = (TextBox)this.PreviousPage.FindControl("txtCity");
     
        lblName.Text = name.Text;
        lblGender.Text = gender.Text;
        lblCity.Text = city.Text;
    }
     
    VB.NET
     
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        Dim name As TextBox = DirectCast(Me.PreviousPage.FindControl("txtName"), TextBox)
        Dim gender As TextBox = DirectCast(Me.PreviousPage.FindControl("txtGender"), TextBox)
        Dim city As TextBox = DirectCast(Me.PreviousPage.FindControl("txtCity"), TextBox)
     
        lblName.Text = name.Text
        lblGender.Text = gender.Text
        lblCity.Text = city.Text
    End Sub
     
    Here you don’t need to check the PreviousPage.IsCrossPagePostBack property. Find controls and display values.
     
  22. Browse the website, provide values in text boxes and click on Third Method button to see the result.

 

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to get and delete multiple GridView rows in ASP.NET

Disclaimer - Privacy
© 2002-2017 DevASP.net