asp.net 4.5 PreviousPage为null,使用母版页进行跨页发布 [英] asp.net 4.5 PreviousPage is null with cross page posting using a master page

查看:89
本文介绍了asp.net 4.5 PreviousPage为null,使用母版页进行跨页发布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:

  • 我正在使用asp.net 4.5
  • 我正在使用母版页
  • 默认情况下已安装FriendlyURLs
  • 我正在尝试使用POST方法跨页发布

在目标网址上,它返回 PreviousPage = null

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

这是由于FriendlyURls吗?

Is this due to FriendlyURls?

来源:

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

目标:

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";
    }

我最终使用此代码解决了我的问题:

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" />

隐藏代码

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

推荐答案

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

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.

这篇关于asp.net 4.5 PreviousPage为null,使用母版页进行跨页发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆