Will Howard
Will Howard

Reputation: 67

asp.net 4.5 PreviousPage is null with cross page posting using a master page

Notes:

On the target url, it comes back with PreviousPage = null

Is this due to FriendlyURls?

source:

<asp:Button ID="btnSubmit" class="btn btn-primary" PostBackUrl="~/NewApplicationConfirmation.aspx" runat="server" Text="Submit" />

target:

protected void Page_Load(object sender, EventArgs e)
    {
        if (PreviousPage != null)
        {
            if (PreviousPage.IsCrossPagePostBack == true)
            {
                var cp = PreviousPage.Master.FindControl("MainContent") as ContentPlaceHolder;
                TextBox PrevinputAppName = cp.FindControl("inputAppName") as TextBox;
                inputAppName.Text = PrevinputAppName.Text;
            }
            else if (PreviousPage.IsCrossPagePostBack == false)
            {
                inputAppName.Text = "page.previouspage.iscrosspagepostback is false";
            }
        }
        else inputAppName.Text = "page.previouspage is null";
    }

EDIT: I eventually used this code to solve my problem:

.aspx

<asp:Button ID="btnSubmit" class="btn btn-primary" onclick="Transfer_Click" runat="server" Text="Submit" />

code-behind

protected void Transfer_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                Server.Transfer("~/NewApplicationConfirmation.aspx");
            }
        }

Upvotes: 1

Views: 1891

Answers (2)

Tim
Tim

Reputation: 4099

You need to add the directive:

<%@ PreviousPageType virtualPath="~/PreviousPage.master"%>

See the documentation from Microsoft on working with PreviousPages.

You could try the tips in this Microsoft article as well

Upvotes: -1

moarboilerplate
moarboilerplate

Reputation: 1643

Your issue is not really due to FriendlyUrls, your issue is because you are using routing in your app (you would have the same issue if you defined routes manually instead of letting FriendlyUrls wire them up for you). If you change the PostbackUrl property to whatever route your app is using for that page (~/NewApplicationConfirmation possibly?) then PreviousPage will no longer be null. However, due to the way ASP.NET validates the previous page, if you have a route alias set up for that page, you may run into more issues. I would say you should either use PreviousPage and no routing, or routing and change your code to look at Request.Form for the values posted to it.

Upvotes: 1

Related Questions