7

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.

Marcel
  • 15,039
  • 20
  • 92
  • 150
brainbolt
  • 616
  • 1
  • 10
  • 24

5 Answers5

7

I found that Friendly URLS might get you this problem. By default, the Web Forms template includes ASP.NET Friendly URLs.

When you use the default WebForm from visual Studio, the AutoRedirectMode is set to Permanent. This makes you request into a "GET" and since you are using Friendly URLS, you can’t evaluate the 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).

  • 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.

Marcel
  • 15,039
  • 20
  • 92
  • 150
Breno
  • 71
  • 1
  • 2
  • If your project not that large, you can create a completely new project and choose **Empty** template instead of **Web Forms** template. Then, create items and copy paste. – wei Nov 02 '18 at 11:19
1

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.

brainbolt
  • 616
  • 1
  • 10
  • 24
-1

I think following article will helps you.

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

there are two method how to use Cross Page Posting PostBack

Chirag
  • 324
  • 2
  • 4
  • 27
  • I read that article before posting my question. It does not offer any solutions to my problem, as far as I can see. Do you see any specific issues with my code? – brainbolt Jan 24 '14 at 07:09
  • your code is fine.i think you can only do this with PreviousPageType directive adding in your page.see link http://www.cosnetics.co.uk/articles/cross-page-posting-not-working/ – Chirag Jan 24 '14 at 07:42
  • Strongly-typing the previous page is not an option in this case. – brainbolt Jan 24 '14 at 07:48
  • In my case I was pointing to wron URL, http://www.webblogsforyou.com/crosspagepostback-access-previouspage-controls-to-nextpage-in-asp-net/ article helps me to do so. – immayankmodi Mar 06 '15 at 05:20
-1

The reason this is happening is simply because you did not set the postbackurl property correctly.

If CrossPagePosting2.aspx is at root of your program change postbackurl to ~/CrossPagePosting1.aspx

You do not need to add the <%@ PreviousPageType %> directive when using postbackurl property. using PreviousPage.FindControl(id) will search the form elements that are posted using postbackurl property

Bee Dev
  • 69
  • 1
  • 4
  • The original problem of the previous page reported as null is not related to the postback url in this case, but to the use of routes (with friendly urls) – Marcel Jul 15 '15 at 10:52
-1

Try this

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