CrossPagePostback in ASP.NET 2.0


What is meant by CrossPagePostback in ASP.NET 2.0?

ASP.NET 2.0 offers a built-in mechanism to override the normal processing cycle and prevent the page from posting back to itself.

Postback may happen in one or the two ways either through the script or through a submit button.

In ASP.NET 2.0, you can configure certain page controls—in particular, those that implement the IButtonControl interface—to post to a different target page. This is referred to as cross-page posting. 

How to implement CrossPagepostback?

It’s simple. You choose the button controls that can cause postback and set their PostBackUrl property.Notice that in this context a button control is any server control that implements IButtonControl.

<asp: button runat=”server” id=”buttonPost”            Text=”Click”            PostBackUrl=”target.aspx” />

When this is set ASP.NET runtime binds the HTML element of the button control with a new Javascript function.Instead of using regular  __doPostback, it uses the new WebForm_DoPostBackWithOptions function. 

PreviousPage

You use the PreviousPage property to reference the posting page and all of its controls.By using the PreviousPage property on the Page class, you can access any input control defined on the posting page.But how to access the protected controls in the PreviousPage on the target page?First we need to add public properties that represents the protected controls andwe need to  use @PreviousPageType property in the target page.For example, imagine that crosspostpage.aspx contains a Textbox named _textBox1. To make it accessible from within a target page, you add the following code to the code-behind class: 

public Textbox TextBox1{    get { return _textBox1; }}

The new TextBox1 property on the page class wraps and exposes the internal text-box control. In the target page we can code as follows

.<@PreviousPageType VirtualPath = “crosspage.aspx”>

The target page can now execute the following code.

Response. Write(PreviousPage.TextBox1.Text); 

Detecting CrossPage Postings

It is not necessary that target page always executes as a postback, it can also be execute as stand alone. To determine this, Page class contains new property called IsCrossPagePostBack .It returns true when page is executing as a result of cross page postback, this behaves exactly same as IsPostBack property.There is every possibility that the target  may invoke through hyperlink, Server.Transfer  etc.When this happens PreviousPage property on the target page returns null.  IsCrossPagePostBack This property returns true if the current page has called some other ASP.NET page. So this property always returns false on the target page. How can a page detect whether it’s being called through a cross-page postback or by any other means? IsCrossPagePostBack property on the PreviousPage object is true for a cross-page posting and false in all other cases. The following sample checks this.

if (!PreviousPage.IsCrossPagePostBack){    …your code}

Advertisements
This entry was posted in ASP.NET. Bookmark the permalink.

One Response to CrossPagePostback in ASP.NET 2.0

  1. Jyothi says:

    Good…………………

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s