The
PreviousPage property can be used when we want to pass values between two aspx
pages.
This property is useful when
you do not want to use Server.Transfer to access value from previous page.
Consider an example, where
we want to pass a label value from one page to another page.
Steps to do:
Main Page
- Place a label and a button control.
- Assign the label value to “Previous” and PostBackUrl of button
control to Next page.
- In the code behind, create a property and return the label value.
Next Page
1. Declare PreviousPageType
directive in the aspx page.
2. Place a label control in
the Next Page.
3. In the Page_Load, if the
page is not postback, and if the previouspage is not null, then assign the
property from previouspage to the label.
Main Page
.aspx
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Value" runat="server" Text="Previous"></asp:Label>
<asp:Button ID="Button1" runat="server" Text="Button" PostBackUrl="~/Next.aspx" />
</div>
</form>
</body>
.aspx.cs
public string Property
{
get
{
return this.Value.Text;
}
}
.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Next.aspx.cs" Inherits="Next" %>
<%@ PreviousPageType VirtualPath="~/Main.aspx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD
XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
.aspx.cs
protected void Page_Load(object
sender, EventArgs e)
{
if (!this.Page.IsPostBack)
{
if
(PreviousPage != null)
{
this.Label1.Text
= (this.PreviousPage as
Main).Property;
}
}
}