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

时间:2015-03-24 16:06:29

标签: c# asp.net cross-page-posting

注意:

  • 我使用的是asp.net 4.5
  • 我正在使用母版页
  • 默认安装FriendlyURL
  • 我正在尝试使用POST方法跨页面帖子

在目标网址上,它会以PreviousPage = null

返回

这是由于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";
    }
编辑:我最终使用此代码来解决我的问题:

的.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");
            }
        }

2 个答案:

答案 0 :(得分:1)

您的问题并非真正归因于FriendlyUrls,您的问题是因为您在应用中使用路由(如果您手动定义路由而不是让FriendlyUrls为您安排路由,则会遇到相同的问题)。如果您将PostbackUrl属性更改为您的应用程序用于该页面的任何路径(可能是〜/ NewApplicationConfirmation?),那么PreviousPage将不再为null。但是,由于ASP.NET验证上一页的方式,如果您为 页面设置了路由别名,则可能会遇到更多问题。我会说你应该使用PreviousPage而不使用路由,或者路由和更改代码以查看Request.Form以发布给它的值。

答案 1 :(得分:-1)

您需要添加指令:

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

有关使用PreviousPages的信息,请参阅Microsoft的documentation

你可以尝试这个Microsoft article中的提示

相关问题