简体   繁体   English

为什么Page.PreviousPage始终为null?

[英]Why is Page.PreviousPage always null?

I am experimenting with cross-page posting by following this MSDN article . 我正在通过这篇MSDN文章试验跨页面发布。 I have this code: 我有这个代码:

CrossPagePosting1.aspx CrossPagePosting1.aspx

<form id="form1" runat="server">
    <h1>Page 1</h1>
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
    <asp:Button ID="Button1" runat="server" Text="Button" PostBackUrl="CrossPagePosting2.aspx"/>
</form>

CrossPagePosting2.aspx CrossPagePosting2.aspx

<form id="form1" runat="server">
    <h1>Page 2</h1>
    <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</form>

CrossPagePosting2.aspx.cs CrossPagePosting2.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    TextBox TextBox1 = (TextBox)Page.PreviousPage.FindControl("TextBox1");
    Label1.Text = TextBox1.Text;
}

This code above produces a NullReferenceException at Page.PreviousPage . 上面的代码在Page.PreviousPage上产生NullReferenceException Why? 为什么?

This is an ASP.Net 4.0 application. 这是一个ASP.Net 4.0应用程序。

It uses FriendlyUrls, which is the default. 它使用FriendlyUrls,这是默认值。

NOTE: I do NOT want the previous page to be strongly-typed, eg using the PreviousPageType directive. 注意:我不希望强大类型的PreviousPageType ,例如使用PreviousPageType指令。 According to the referenced article, this shouldn't be necessary. 根据引用的文章,这不应该是必要的。

I found that Friendly URLS might get you this problem. 我发现友情URL可能会让你遇到这个问题。 By default, the Web Forms template includes ASP.NET Friendly URLs. 默认情况下,Web窗体模板包含ASP.NET友好URL。

When you use the default WebForm from visual Studio, the AutoRedirectMode is set to Permanent. 使用Visual Studio中的默认WebForm时,AutoRedirectMode设置为Permanent。 This makes you request into a "GET" and since you are using Friendly URLS, you can't evaluate the PreviousPage. 这使您请求进入“GET”,并且由于您使用的是友好URL,因此无法评估PreviousPage。

Workarounds: 解决方法:

  • If you want a "POST" action then set the AutoRedirectMode = RedirectMode.Off (this will give you PreviousPage info but only from non-Friendly-Url pages [ex: www.you.com/mypage.aspx], however this will get you an error if you try to access the Friendly-Url page [ex: www.you.com/mypage] << no .aspx). 如果你想要一个“POST”动作然后设置AutoRedirectMode = RedirectMode.Off(这将给你PreviousPage信息,但只能从非友好的Url页面[例如:www.you.com/mypage.aspx],但是这将得到如果您尝试访问Friendly-Url页面[ex:www.you.com/mypage] << no .aspx),则会出现错误。

  • If you want the PreviousPage information you will have to set the previous post directive in you webpage <%@ PreviousPageType VirtualPath="~/Page1.aspx" %> OR maybe use the Server.Transfer in a OnClick Method. 如果您需要PreviousPage信息,则必须在您的网页中设置上一个post指令<%@ PreviousPageType VirtualPath =“〜/ Page1.aspx”%>或者可以在OnClick方法中使用Server.Transfer。

这里的问题是由FriendlyUrls引起的,它默认安装在我正在使用的测试站点上。我禁用了FriendlyUrls,它运行良好。

I think following article will helps you. 我认为以下文章将对您有所帮助。

http://csharpdotnetfreak.blogspot.com/2009/08/cross-page-posting-in-aspnet.html http://csharpdotnetfreak.blogspot.com/2009/08/cross-page-posting-in-aspnet.html

there are two method how to use Cross Page Posting PostBack 有两种方法如何使用Cross Page Posting PostBack

The reason this is happening is simply because you did not set the postbackurl property correctly. 发生这种情况的原因很简单,因为您没有正确设置postbackurl属性。

If CrossPagePosting2.aspx is at root of your program change postbackurl to ~/CrossPagePosting1.aspx 如果CrossPagePosting2.aspx位于您的程序的根目录,请将postbackurl更改为~/CrossPagePosting1.aspx

You do not need to add the <%@ PreviousPageType %> directive when using postbackurl property. 使用postbackurl属性时,不需要添加<%@ PreviousPageType%>指令。 using PreviousPage.FindControl(id) will search the form elements that are posted using postbackurl property 使用PreviousPage.FindControl(id)将搜索使用postbackurl属性发布的表单元素

Try this 试试这个

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack && PreviousPage != null)
        {
            Page page = PreviousPage;
            Label1.Text = ((TextBox)page.FindControl("TextBox1")).Text;
        }
    }

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

相关问题 为什么 Page.PreviousPage 超时,是 sessionState 超时还是别的什么? - Why Page.PreviousPage timedout, is it sessionState timeout or something else? 通过HttpRequest进行ASP.NET跨页发布 Page.PreviousPage? - ASP.NET Cross Page Posting via HttpRequest || Page.PreviousPage? 在交叉发布中,PreviousPage = null - PreviousPage = null in crosspage posting asp.net 4.5 PreviousPage为null,使用母版页进行跨页发布 - asp.net 4.5 PreviousPage is null with cross page posting using a master page PreviousPage在一台机器上为空,而在另一台机器上为空 - PreviousPage is null on one machine, not on the other 使用按钮和PostBackURL属性尝试跨页回发时,PreviousPage为null - PreviousPage is null when attempting a cross-page postback using a button and the PostBackURL attribute 使用上一页,为什么在使用try ... catch时收到“调用方法之前对象为空...”的消息? - Using Previouspage, why do i get “the object is null before calling a method…” message while using try…catch? 使用“PreviousPage”从源页面获取值 - Getting values from the source page using “PreviousPage” 为什么PageIoad处理程序中的DataItem始终为null? - Why is DataItem always null in the Page_Load handler? “正常回发”和“页面创建为 PreviousPage”之间的区别 - Difference between “normal postback” and “page creation as PreviousPage”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM