Search code examples
asp.netwebformscross-page-posting

Why is Page.PreviousPage always null?


I am experimenting with cross-page posting by following this MSDN article. I have this code:

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

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

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. Why?

This is an ASP.Net 4.0 application.

It uses FriendlyUrls, which is the default.

NOTE: I do NOT want the previous page to be strongly-typed, e.g. using the PreviousPageType directive. According to the referenced article, this shouldn't be necessary.


Solution

  • The problem here was being caused by FriendlyUrls, which were installed by default on the test site I was working in. I disabled FriendlyUrls, and it worked.