.NET Framework Class Library
Page..::.PreviousPage Property

Updated: October 2009

Gets the page that transferred control to the current page.

Namespace:  System.Web.UI
Assembly:  System.Web (in System.Web.dll)
Syntax

Visual Basic (Declaration)
<BrowsableAttribute(False)> _
Public ReadOnly Property PreviousPage As Page
Visual Basic (Usage)
Dim instance As Page
Dim value As Page

value = instance.PreviousPage
C#
[BrowsableAttribute(false)]
public Page PreviousPage { get; }
Visual C++
[BrowsableAttribute(false)]
public:
property Page^ PreviousPage {
    Page^ get ();
}
JScript
public function get PreviousPage () : Page

Property Value

Type: System.Web.UI..::.Page
The Page representing the page that transferred control to the current page.
Exceptions

ExceptionCondition
InvalidOperationException

The current user is not allowed to access the previous page.

-or-

ASP.NET routing is in use and the previous page's URL is a routed URL. When ASP.NET checks access permissions, it assumes that the URL is an actual path to a file. Because this is not the case with a routed URL, the check fails.

Remarks

When you use the Transfer method or use cross-page posting to transfer processing from one ASP.NET page to another, the originating page contains request information that might be required for the destination page. You can use the PreviousPage property to access that information

If the current page is being rendered as a result of a direct request (not a transfer or cross-post from another page), the PreviousPage property contains nullNothingnullptra null reference (Nothing in Visual Basic).

TopicLocation
How to: Determine How ASP.NET Web Pages Were InvokedBuilding ASP .NET Web Applications
How to: Determine How ASP.NET Web Pages Were InvokedBuilding ASP .NET Web Applications
How to: Determine How ASP.NET Web Pages Were InvokedBuilding ASP .NET Web Applications in Visual Studio
How to: Determine How ASP.NET Web Pages Were InvokedBuilding ASP .NET Web Applications in Visual Studio
How to: Pass Values Between ASP.NET Web PagesBuilding ASP .NET Web Applications
How to: Pass Values Between ASP.NET Web PagesBuilding ASP .NET Web Applications
How to: Pass Values Between ASP.NET Web PagesBuilding ASP .NET Web Applications in Visual Studio
How to: Pass Values Between ASP.NET Web PagesBuilding ASP .NET Web Applications in Visual Studio
Examples

The following example is in two parts. The first is an ASP.NET page that uses the Transfer method, exposed in the page model as Server.Transfer("path"). The second part is the target page, which uses the PreviousPage property to get the title of the first page.

Visual Basic
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    Protected Sub Page_Load(ByVal sender As Object, _
        ByVal e As EventArgs)

        Dim IsServerAvailable As Boolean

        ' If second is an even number, the server is available
        ' Replace this line with a valid check for the server.
        IsServerAvailable = (DateTime.Now.Second Mod 2 = 0)

        If Not IsServerAvailable Then
            Server.Transfer("Notify.aspx", True)
        End If
    End Sub
</script>

<html  >
<head runat="server">
    <title>Switch Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h2>Database Server is Available</h2>

    <p>This page appears if the database server 
        is available.</p>

    <p>Enter a pretend Server Name: 
        <asp:TextBox ID="serverNameText" 
        runat="server">MyDatabaseServer</asp:TextBox>
    </p>

    <p><asp:Button ID="SubmitButton" runat="server" 
        Text="Is server available?" /></p>
    </div>
    </form>
</body>
</html>
C#
<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    protected void Page_Load(object sender, EventArgs e)
    {
        // If second is an even number, the server is available
        // Replace this line with a valid check for the server.
        bool IsServerAvailable = (DateTime.Now.Second % 2 == 0);

        if (!IsServerAvailable)
            Server.Transfer("Notify.aspx", true);
    }
</script>

<html  >
<head runat="server">
    <title>Switch Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h2>Database Server is Available</h2>

    <p>This page appears if the database server 
        is available.</p>

    <p>Enter a pretend Server Name: 
        <asp:TextBox ID="serverNameText" 
        runat="server">MyDatabaseServer</asp:TextBox>
    </p>

    <p><asp:Button ID="SubmitButton" runat="server" 
        Text="Is server available?" /></p>
    </div>
    </form>
</body>
</html>
Visual Basic
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    Protected Sub Page_Load(ByVal sender As Object, _
        ByVal e As EventArgs)

        Dim txt As TextBox

        ' Find the server name on the previous page
        txt = CType(Page.PreviousPage.FindControl _
            ("serverNameText"), TextBox)
        If Not IsNothing(txt) Then
            prevServerName.Text = Server.HtmlEncode(txt.Text)
        Else
            prevServerName.Text = "[Name Not available]"
        End If
    End Sub
</script>

<html  >
<head runat="server">
    <title>Page A</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h2>Database Server is Not Available</h2>

    <p>This page appears if the named database server is not 
    available, but the URL displays as the main target page.</p>

    <p>Server Name (From Page.PreviousPage): 
        <asp:Label ID="prevServerName" runat="server" /></p>

    <p>Refresh the page to see if the server is now available.</p>
    </div>
    </form>
</body>
</html>
C#
<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    protected void Page_Load(object sender, EventArgs e)
    {
        // Find the server name on the previous page
        TextBox txt = 
                (TextBox)Page.PreviousPage.FindControl("serverNameText");
        if (txt != null)
            prevServerName.Text = Server.HtmlEncode(txt.Text);
        else
            prevServerName.Text = "[Name Not available]";
    }
</script>

<html  >
<head runat="server">
    <title>Page A</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h2>Database Server is Not Available</h2>

    <p>This page appears if the named database server is not 
    available, but the URL displays as the main target page.</p>

    <p>Server Name (From Page.PreviousPage): 
        <asp:Label ID="prevServerName" runat="server" /></p>

    <p>Refresh the page to see if the server is now available.</p>
    </div>
    </form>
</body>
</html>
Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0
See Also

Reference

Change History

Date

History

Reason

October 2009

Added notes about when the property might be nullNothingnullptra null reference (Nothing in Visual Basic) and about exceptions that might be raised when ASP.NET routing is used.

Customer feedback.

Tags :


Page view tracker