In this example i am going to describe Cross Page Posting or postback in ASP.NET 2.0 / 3.5 using C# and VB.NET.
Cross Page posting or cross page postback 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
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
VB.NET Code behind 
If you are using masterpages then you need to write code to FindControl as mentioned below
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
VB.NET
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) 
Now write this code in page_Load event of second page to retrieve values of controls 
C# code
VB Code
Hope this helps
Cross Page posting or cross page postback 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
<%@ 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"
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
01protected void Page_Load(object sender, EventArgs e)02{03    //Check whether previous page is cross page post back or not04    if (PreviousPage != null && PreviousPage.IsCrossPagePostBack)05    {06        TextBox txtPbFirstName = (TextBox)PreviousPage.FindControl("txtFirstName");07        TextBox txtPbLastName = (TextBox)PreviousPage.FindControl("txtLastName");08        Label1.Text = "Welcome " + txtPbFirstName.Text + " " + txtPbLastName.Text;09    }10    else11    {12        Response.Redirect("Default.aspx");13    }14}01Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)02    'Check whether previous page is cross page post back or not03    If PreviousPage IsNot Nothing AndAlso PreviousPage.IsCrossPagePostBack Then04        Dim txtPbFirstName As TextBox = DirectCast(PreviousPage.FindControl("txtFirstName"), TextBox)05        Dim txtPbLastName As TextBox = DirectCast(PreviousPage.FindControl("txtLastName"), TextBox)06        Label1.Text = ("Welcome " & txtPbFirstName.Text & " ") + txtPbLastName.Text07    Else08        Response.Redirect("Default.aspx")09    End If10End SubIf you are using masterpages then you need to write code to FindControl as mentioned below
1ContentPlaceHolder exampleHolder =(ContentPlaceHolder)Page.PreviousPage.Form.FindControl ("Content1"));2TextBox 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
01public TextBox pbTxtFirstName02    {03        get04        {05            return txtFirstName;06        }07    }08 09    public TextBox pbTxtLastName10    {11        get12        {13            return txtLastName;14        }15    }VB.NET
01Public ReadOnly Property pbTxtFirstName() As TextBox02    Get03        Return txtFirstName04    End Get05End Property06 07Public ReadOnly Property pbTxtLastName() As TextBox08    Get09        Return txtLastName10    End Get11End Property<%@ PreviousPageType VirtualPath="~/Default.aspx" %>C# code
01protected void Page_Load(object sender, EventArgs e)02{03    if (PreviousPage != null && PreviousPage.IsCrossPagePostBack)04    {05        Label1.Text = "Welcome " + PreviousPage.pbTxtFirstName.Text + " " + PreviousPage.pbTxtLastName.Text;06    }07    else08    {09        Response.Redirect("Default.aspx");10    }11}1Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)2    If PreviousPage IsNot Nothing AndAlso PreviousPage.IsCrossPagePostBack Then3        Label1.Text = ("Welcome " & PreviousPage.pbTxtFirstName.Text & " ") + PreviousPage.pbTxtLastName.Text4    Else5        Response.Redirect("Default.aspx")6 7    End If8End SubHope this helps
 
 
0 comments:
Post a Comment