locked
PreviousPage is always null RRS feed

  • Question

  • User-114420135 posted

    Hello,

    I am trying to pass values between two aspx page using PreviousPage; I tried the suggested two ways: 

    • Adding PostBackUrl to the button in the first page to go to go to the second page. Then using the Page.PreviousPage.FindControlRecursive in the Page_Load method of the second page code behind to get the value of the fields in the first page.
    • Adding a get method in the first page code behind and then adding the directive <%@ PreviousPageType VirtualPath="~/Default.aspx" %> in the second page; then using PreviousPage.field to access the field.

    Seems like whatever I do, PreviousPage is always null.....looked into internet but nothing seems to help my case. 

    Mainly I followed http://msdn.microsoft.com/en-us/library/6c3yckfw(v=vs.140).aspx

    Your suggestions would be highly appreciated.

    Kind Regards,

    Chitra

    Tuesday, January 6, 2015 7:19 AM

Answers

  • User555306248 posted

    If you are directly calling that url then it will return null if you are coming to that page by redirecting from any other page then you will get it

    refer this

    http://babasahebkale.blogspot.in/2011/08/get-previous-page-url.html

    if (this.Page.Request.UrlReferrer != null)

    {

          string previousPage = Request.UrlReferrer.ToString(); }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 7, 2015 10:53 PM

All replies

  • User753101303 posted

    Hi,

    This one is really general. Try rather to fllow http://msdn.microsoft.com/en-us/library/ms178139(v=vs.140).aspx possibly with just the code shown until you can see it is not null anymore. Then only move on adding controls and getting their values from the previous page.

    Tuesday, January 6, 2015 7:24 AM
  • User-114420135 posted

    Hello,

    Thanks for replying. The PreviousPage value is null in the first statement of the target page, so dont understand whats happening. 

    Seemed to me very simple to do but apparently its not :(.

    Lost in my way.

    Kind Regards,

    Chitra

    Tuesday, January 6, 2015 7:35 AM
  • User753101303 posted

    So try something really basic such as:

      <asp:Button runat="server" Text="OK"  PostBackUrl="~/WebForm2.aspx"/>

    On form2 I just show the value:

     protected void Page_Load(object sender, EventArgs e)
            {
                lbl.Text = PreviousPage.IsCrossPagePostBack.ToString();
            }

    Does it work? It does here. Get rid of everything else that is not basic.

    Tuesday, January 6, 2015 7:45 AM
  • User-183374066 posted

    Hi Chitra ..

    Please follow instructions given in this article

    http://msdn.microsoft.com/en-us/library/ms178139%28v=vs.140%29.aspx

    You can set postback url to your button to redirect to target page

    <asp:Button ID="Button1" PostBackUrl="~/TargetPage.aspx" runat="server" Text="Submit" />

    and in your target page on page load event check

    if(PreviousPage != null)
    {
        if(PreviousPage.IsCrossPagePostBack == true)
        {
             Label1.Text = "Cross-page post.";
        }
    }
    else
    {
        Label1.Text = "Not a cross-page post.";
    }

    also check in page load for Page.IsCrossPagePostBack Property

    Tuesday, January 6, 2015 8:02 AM
  • User-114420135 posted

    Hello,

    Thanks for your reply.

    I tried with a new empty project and its working when the aspx page is not a content page but in my initial case the aspx page is a content page with master pages where it does not work. 

    May be I need to do something different for access to previous page for content pages?

    Kind Regards,

    Chitra

    Tuesday, January 6, 2015 9:07 AM
  • User753101303 posted

    Tried and PreviousPage is not null (be always explicit. Do you mean controls are not found or that PreviousPage is still null).

    Just have:

    <%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="WebForm3.aspx.cs" Inherits="WebApplication9.WebForm3"  %>
    <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
                <asp:Button ID="Button1" runat="server" Text="OK"  PostBackUrl="~/WebForm2.aspx"/>
    </asp:Content>

    And webform2.aspx page is unchanged and I still see PreviousPage.IsCrossPagePostBack being true... Really try the most basic setup you can...

    Tuesday, January 6, 2015 9:20 AM
  • User-114420135 posted

    Hello,

    Thanks for your prompt reply. I just did exactly what u said and its definitely not working for me :(..... The code is as follows:

    For the calling page (Default.aspx):

    <%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication6._Default" %>
    
    <asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
        <asp:Button ID="Button1" runat="server" Text="OK" PostBackUrl="~/WebForm1.aspx" />
    </asp:Content>

    Target Page(WebForm1.aspx):

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication6.WebForm1" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:Label ID="lbl" runat="server" Text="Label"></asp:Label>
        </div>
        </form>
    </body>
    </html>

    Code behind in target page(WebForm1.aspx.cs):

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace WebApplication6
    {
        public partial class WebForm1 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                lbl.Text = PreviousPage.IsCrossPagePostBack.ToString();
            }
        }
    }

    I used debug and definitely previousPage is getting the value of null.

    Kind Regards,

    Chitra

    Tuesday, January 6, 2015 9:41 AM
  • User753101303 posted

     Is your master page also streamlined? Could you have some javascript code that would alter the behavior or maybe a misplaced "form" tag there? It would explain also why it works for you with a content page but not when using a master page (ie the issue is caused by the master page itself ?)

    Tuesday, January 6, 2015 9:55 AM
  • User-114420135 posted

    Hello,

    I created a brand new project with type 'ASP.NET Web Application' and 'Web Forms' Template in order to simulate the problem. It didn't work when I put the code there.

    When I did the same to create another new project with no template(which does not create master pages) and put the same code(as above), it worked. 

    So that's why I thought that the master page is the problem.

    Kind Regards,

    Chitra

    Tuesday, January 6, 2015 10:03 AM
  • User1918509225 posted

    Hi chitralekha,

    I have tested your code in my side , it works very well in my side.

    If you don't mind, please share your project which can reproduce  your issue on onedrive(https://onedrive.live.com/), so that I can test it in my side.

    Best Regards,

    Kevin Shen.

    Wednesday, January 7, 2015 12:44 AM
  • User-114420135 posted

    Hello Kevin,

    Thanks for your reply.

    The link is:

    https://onedrive.live.com/embed?cid=72C222E24A94C576&resid=72C222E24A94C576%21106&authkey=AAtwJpcgOUOUx8g

    Please let me know if you can see the problem in it? PreviousPage is still null for me.

    Kind Regards,

    Chitra 

    Wednesday, January 7, 2015 5:39 AM
  • User555306248 posted

    If you are directly calling that url then it will return null if you are coming to that page by redirecting from any other page then you will get it

    refer this

    http://babasahebkale.blogspot.in/2011/08/get-previous-page-url.html

    if (this.Page.Request.UrlReferrer != null)

    {

          string previousPage = Request.UrlReferrer.ToString(); }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 7, 2015 10:53 PM