1

Notes:

  • I am using asp.net 4.5
  • I am using a master page
  • FriendlyURLs is installed by default
  • I am trying to cross page post using the POST method

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");
            }
        }
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

2 Answers2

1

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.

moarboilerplate
  • 1,633
  • 9
  • 23
  • What a mess. To avoid this I have have used server.transfer -- which avoids (for some reason) the issue and previouspage works and findcontrol works and I don't need to disable friendlyurls and routing works. I think you are correct that "the issue here appears to me that OP is getting bumped to an extensionless url so when the framework looks for cross-page postbacks going to the extensionless url, it can't find any, because the destination without the extension is technically not the same as the page defined in the PostbackUrl." – Will Howard Mar 25 '15 at 14:40
  • @WillHoward You may run into issues with back, etc. with `Server.Transfer` depending on how you've implemented it. It might be better to switch your implementation to use `Request.Form["input"]` instead of using `FindControl` on `PreviousPage`. Or you could have it post back to the same page (though I'm sure you have a reason for doing a cross-page post). – moarboilerplate Mar 25 '15 at 15:42
-1

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

Tim
  • 4,051
  • 10
  • 36
  • 60
  • This will only make the `PreviousPage` property a strongly-typed property, but it will still be null. – moarboilerplate Mar 24 '15 at 19:25
  • Actually, I thought you were talking about using the `PreviousPageType` directive. This will only strongly-type the Master page, which isn't even being referenced by the code? – moarboilerplate Mar 24 '15 at 19:26
  • D'oh! Looked up the wrong type...(need...more...coffee...) – Tim Mar 24 '15 at 19:28
  • I've never gotten PreviousPage to work without strongly typing it...but I'm on .NET 4.0, I don't know if this has changed in 4.5...and since the OP is trying to get to the master on the previous page, I think he needs a reference to the master there too. – Tim Mar 24 '15 at 19:30
  • Yeah if you need to get at a specific property of the page, you need to strongly type it. As for the master page, OP can't even get to that area of the code, but it shouldn't need to be typed since the MasterPage type implements control (exposing FindControl). Anyway, the issue here appears to me that OP is getting bumped to an extensionless url so when the framework looks for cross-page postbacks going to the extensionless url, it can't find any, because the destination without the extension is technically not the same as the page defined in the PostbackUrl. – moarboilerplate Mar 24 '15 at 19:33