ASP.NET - Directives - PreviousPageType
The PreviousPageType directive is used in ASP.NET to enable communication between pages. Specifically, it allows the content page to access public properties or methods of the source page that redirected to it.
Here's an example of how to use the PreviousPageType directive in an ASP.NET content page:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="MyApplication.Default" %>
<%@ PreviousPageType VirtualPath="~/SourcePage.aspx" %>
<!DOCTYPE html>
<html>
<head>
<title>Content Page</title>
</head>
<body>
<h1>Content Page</h1>
<p>Previous page title: <%= PreviousPage.Title %></p>
</body>
</html>
In this example, the PreviousPageType directive specifies the virtual path of the source page, which is the page that redirected to this content page. The source page must have a public property named Title that can be accessed by the content page.
Here's an example of a source page that defines a public property:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SourcePage.aspx.cs" Inherits="MyApplication.SourcePage" %>
<!DOCTYPE html>
<html>
<head>
<title>Source Page</title>
</head>
<body>
<h1>Source Page</h1>
</body>
</html>
public partial class SourcePage : System.Web.UI.Page
{
public string Title { get { return "Source Page Title"; } }
protected void Page_Load(object sender, EventArgs e)
{
}
}
In this example, the Title property returns a string value that represents the title of the source page.
When the user navigates from the source page to the content page, the Title property of the source page is accessed by the content page and displayed in a paragraph element using the <%= ... %> syntax.
Note that to use the PreviousPageType directive, the source page and the content page must be part of the same ASP.NET application and running on the same server. Additionally, the source page must have previously been loaded in the user's browser before the content page is loaded, as the PreviousPage property is only available if the user arrived at the content page through a redirect from the source page.