Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Asp.net 2.0 Cross Post backing

0.00/5 (No votes)
27 Feb 2010 1  
This is new Concept Introduce in Asp.net 2.0, This is used to Passing the values from one page to Certain page using PostbakUrl property for Button,

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

This is new Concept Introduce in Asp.net 2.0, This is used to Passing the values from one page to Certain page using PostbakUrl property for Button, LinkButton, and Image Button from that easily values can be access by prevoius page

Below is example For the Cross Page posting,

This can be achieved following 3 ways 

1)Server.Transfer("PageUrl");

2)Response.redirect(" PageUrl")

3)PostbackUrl("PageUrl")

 

This Example Contains Source.aspx and Destination.aspx ,2 Pages

In Source.aspx

------------------------------

First Name:<asp:TextBox ID="FirstName" runat="server"></asp:TextBox><br />

            Last Name:<asp:TextBox ID="LastName" runat="server"></asp:TextBox><br />
        <br />
        <asp:ImageButton ID="ImageButton1"  ImageUrl="~/Image.jpg"  Height="30" Width="40" runat="server"               PostBackUrl ="~/DestinationPage.aspx" />
        <asp:LinkButton ID="LinkButton1" runat="server"    PostBackUrl="~/DestinationPage.aspx">LinkButton</asp:LinkButton>
            <asp:Button ID="Button1" runat="server" Text="Submit To Destination Page"
             PostBackUrl ="~/DestinationPage.aspx" /><!--onclick="Button1_Click1" />

---------------------------------

In Source.aspx.cs

public string FormFirstName
    {

        get { return FirstName.Text; }

    }



    public string FormLastName
    {

        get { return LastName.Text; }

    }
    protected void Page_Load(object sender, EventArgs e)
    {

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

            Page previousPage = PreviousPage;

            TextBox firstName = (TextBox)previousPage.FindControl("FirstName");

            TextBox lastName = (TextBox)previousPage.FindControl("LastName");
}

protected void Button1_Click1(object sender, EventArgs e)
    {
        Server.Transfer("DestinationPage.aspx");

    }

 -------------------------------

 DestinationPage.aspx

<asp:Label ID="labelFirstName" runat="server" Text="Label"></asp:Label>
    <br />
    <asp:Label ID="labelLastName" runat="server" Text="Label"></asp:Label>
        <asp:TextBox ID="nametxt" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Button"/>

 

--------------------------------

DestinationPage.aspx.cs

protected void Page_Load(object sender, EventArgs e)
    {
        string str = Request.Form["FirstName"];
           
        //if ((PreviousPage != null) || (PreviousPage.IsCrossPagePostBack))
            if((!Page.IsPostBack) || (PreviousPage.IsCrossPagePostBack))
        {
            SourcePage prevPage = PreviousPage;

            labelFirstName.Text = prevPage.FormFirstName;

            labelLastName.Text = prevPage.FormLastName;
        }


        else if ((PreviousPage!=null) ||(PreviousPage.IsCrossPagePostBack))

            {

                SourcePage prevPage = PreviousPage;



                // we can now use the values from textboxes and display them in two Label controls..

                labelFirstName.Text = prevPage.FormFirstName;

                labelLastName.Text = prevPage.FormLastName;
             }
       
    }

 

 

 

 

 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here