简体   繁体   English

asp.net 4.5 PreviousPage为null,使用母版页进行跨页发布

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

Notes: 笔记:

  • I am using asp.net 4.5 我正在使用asp.net 4.5
  • I am using a master page 我正在使用母版页
  • FriendlyURLs is installed by default 默认情况下,FriendlyURLs已安装
  • I am trying to cross page post using the POST method 我正在尝试使用POST方法跨页面发布

On the target url, it comes back with PreviousPage = null 在目标网址上,它返回PreviousPage = null

Is this due to FriendlyURls? 这是由于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 的.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");
            }
        }

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). 您的问题并非是由于FriendlyUrl引起的,实际上是因为您在应用程序中使用了路由(如果手动定义路由而不是让FriendlyUrl为您连接它们,则会遇到相同的问题)。 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. 如果将PostbackUrl属性更改为应用程序用于该页面的任何路由(可能使用〜/ NewApplicationConfirmation?),则PreviousPage将不再为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. 但是,由于ASP.NET验证上一页的方式,如果为该页面设置了路由别名,则可能会遇到更多问题。 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. 我会说您应该使用PreviousPage而不进行路由,或者路由并更改代码以查看Request.Form中发布到其中的值。

You need to add the directive: 您需要添加指令:

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

See the documentation from Microsoft on working with PreviousPages. 请参阅Microsoft有关使用PreviousPages的文档

You could try the tips in this Microsoft article as well 您也可以尝试这篇Microsoft文章中的技巧

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM