How To -Cross page postbacks in ASP.NET 2.0

ASP.NET 2.0 introduces the ability to have an ASPX page postback to a different ASPX page with cross page postbacks. This was done all the time in ASP but wasn’t supported in ASP.NET 1.x. This wasn’t directly supported because on the server you need the same page to be recreated upon postback so the same controls could be repopulated with the post data. Since the model is to work at a higher level with server side controls and their properties, posting back to a different page would force you to access Request.Form to fetch the data a user had entered, which is a lower level than the control model wants.

So, in v2.0 they came up with a way to support cross-page postbacks. This is enabled by a button on the first page setting the property to the page that will handle the postback. Once on the second page, you can access the controls from the previous page by accessing the Page.PreviousPage property. Here’s a sample

CrossPagePostBackSource.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CrossPagePostBackSource.aspx.cs"
    Inherits="CrossPagePostBackSource" %
    >

<!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>
            <asp:TextBox runat="server" ID="_tb1"></asp:TextBox>
            +
            <asp:TextBox runat="server" ID="_tb2"></asp:TextBox>
            *
            <asp:TextBox runat="server" ID="_tb3"></asp:TextBox>
            <asp:Button runat="server" ID="_button" PostBackUrl="~/TargetPage.aspx" Text=" = " />
        </div>
    </form>
</body>
</html>

CrossPagePostBackSource.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class CrossPagePostBackSource : System.Web.UI.Page
{
    private int sum;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    public int Sum
    {
        get
        {
            return ((Int32.Parse(_tb1.Text) + Int32.Parse(_tb2.Text)) * Int32.Parse(_tb3.Text));
        }
    }
}

TargetPage.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TargetPage.aspx.cs" Inherits="TargetPage" %>

<%@ PreviousPageType VirtualPath="~/CrossPagePostBackSource.aspx" %>
<!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>
            Answer is:
            <asp:Label runat="server" ID="_result"></asp:Label>
        </div>
    </form>
</body>
</html>

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class TargetPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    
        _result.Text = PreviousPage.Sum.ToString();

    }
}

Post a Comment

Please do not post any spam link in the comment box😊

Previous Post Next Post

Blog ads

CodeGuru