<%@ PreviousPageType %> Directive:
In the above example, to access a control that is declared in source
page we have to redeclare it in the target page. ASP.Net 2.0 has a
PreviousPageType directive to redeclare the source page in the target page. Also, we
have to declare a public property for every control in source page to access it in the target page.
<%@ PreviousPageType
VirtualPath="~/Default.aspx" %>
So the code will be like,
SourcePage:
<asp:TextBox ID="txtDOB"
runat="server"></asp:TextBox>
<asp:Button ID="btnGeturAge"
runat="server" PostBackUrl="~/Age.aspx"
Text="Calculate Age" />
public DateTime DOB
{
get
{
return Convert.ToDateTime(txtDOB.Text);
}
}
TargetPage:
<%@ PreviousPageType VirtualPath="~/SourcePage.aspx”
%>
protected void Page_Load(object sender, EventArgs e)
{
DateTime dt=PreviousPage.DOB;
}
When the source page is posted to the target page, the target
page will get executed for the first time which makes target page’s IsPostback
to be false.The source page will be loaded into the memory and executes all the
events except the render event. To ensure that the page is executed for a cross
page postback there is a property called IsCrossPagePostBack in PreviousPage object
which will be true for a cross page postback.
PreviousPage.IsCrossPagePostBack
So we can write the above code like,
if (PreviousPage.IsCrossPagePostBack)
{
DateTime dt=PreviousPage.DOB;
}
|