Passing the PreviousPage in ASP.NET web pages.

In ASP.NET web pages, we can use Cross-Page Posting with PreviousPage to access the content of the previous page. Cross-Page Posting with PreviousPage involves transferring control to another page using Server.Transfer or Response.Redirect methods, while passing the content of the current page to the target page. Here is an example code snippet using Cross-Page Posting with PreviousPage:

Page One (Default.aspx):

<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<asp:Button ID="btnNext" runat="server" Text="Next" OnClick="btnNext_Click" />

In the Click event of the button, transfer control to the second page using the Server.Transfer method and pass the content of the current page to the second page.

protected void btnNext_Click(object sender, EventArgs e)
{
    Server.Transfer("SecondPage.aspx", true);
}

Page two (SecondPage.aspx):

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

In the Page_Load event on the second page, use the PreviousPage property to access the content of the previous page and display it on the Label control.

protected void Page_Load(object sender, EventArgs e)
{
    if (PreviousPage != null)
    {
        TextBox txtName = (TextBox)PreviousPage.FindControl("txtName");
        if (txtName != null)
        {
            lblName.Text = txtName.Text;
        }
    }
}

In this example, when the user enters their name on the first page and clicks the Next button, control will be transferred to the second page, displaying the name input from the previous page. Using cross-page posting with PreviousPage allows data to be passed without using Session or QueryString.

Leave a Reply 0

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


广告
Closing in 10 seconds
bannerAds