CODEDIGEST
Home » Articles
Search
 

Technologies
 

Sponsored links
 

CodeDigest Navigation
 

Technology News
No News Feeds available at this time.
 

Community News
No News Feeds available at this time.
 
Cross Page Post Back in ASP.Net 2.0

By Satheeshbabu
Posted On Dec 25,2007
Article Rating:
Be first to rate
this article.
No of Comments: 0
Category:
Print this article.

Cross Page Post Back in ASP.Net 2.0

 

In ASP.Net 1.x, an ASPX page cannot be posted to a different ASPX page. ASP.Net 2.0 overcame this drawback by providing us the feature, cross page post back. We can now post an ASPX page to a different page with minimal effort.
It can be achieved through the new property that comes with the button control.

 

btnGeturAge.PostBackUrl = "Target.aspx";

 

Refer the below figure which shows the button's PostBackUrl property in visual studio.

How to access the controls of source page from the target Page?

 

ASP.Net 2.0 Page object now comes with a property called PreviousPage which gives the access to the posted page.

 

Page P=Page.PreviousPage;

 

 

The Page object comes with a method called FindControl() which takes the control ID as parameter and gives the access to that control.

 

TextBox text=(TextBox)P.FindControl(“txtDOB”);

 

Or

 

TextBox text=(TextBox)PreviousPage.FindControl(“txtDOB”);

 

The code for the source and target page will be,

 

SourcePage:

 

<asp:TextBox ID="txtDOB" runat="server"></asp:TextBox>

<asp:Button ID="btnGeturAge" runat="server" PostBackUrl="~/Target.aspx" Text="Calculate Age" />

 

TargetPage:

 

protected void Page_Load(object sender, EventArgs e)

{

Page P=Page.PreviousPage;

TextBox text=(TextBox)P.FindControl(“txtDOB”);

}

 




<%@ 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;

}

What happens in cross page postback?

 

ASP.Net .2.0 will add the below Jscript function for the button that handles the cross page postback.This function makes the source form to be posted on the Target page.

 

onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;btnGeturAge&quot;, &quot;&quot;, false, &quot;&quot;, &quot;Target.aspx&quot;, false, false))"

 

Download source:

Download source
Similar Articles
You can contribute to CodeDiget.Com:
Donate to CodeDigest.com
Article Feedback
Comments