AspAlliance.com LogoASPAlliance: Articles, reviews, and samples for .NET Developers
URL:
http://aspalliance.com/articleViewer.aspx?aId=1040&pId=-1
Working with Cross Page Posting Using ASP.NET 2.0
page
by SANJIT SIL
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 122000/ 250

Introduction

Cross Page Posting is a new feature in ASP.NET 2.0.  By using this feature we can submit a form (say crosspage1.aspx) and post the form along with all its control values into another page (say crosspage2.aspx).  Using this feature is straightforward and simple.  We have to set PostBackUrl property on any web controls that implement the System.Web.UI.WebControls.IButtonControl   interface, which includes Button, ImageButton and LinkButton.

Different methods to post data

There are several methods to send data from one page to another page.

·         Response.Redirect

To perform client side redirection in ASP.NET, users can call Response.Redirect and pass the URL. When Response.Redirect is called, the server sends a command back to the browser telling it to request the page redirected to it.  An extra roundtrip happens, which hit the performance.  We can send information from the source page by using a query string.  There is a limitation on the length of a query string; it cannot be used to pass large amounts of data over the wire. 

·         Cookies

We can store data using cookies.  Cookies are created on the server side, but saved on the client side.  Cookies can be deleted from client side.

·         Session Variables :

 We can store the source page information in the session and retrieve it from the target page.  However, this requires saving the information on the server so the server memory load can be increased.

·         Server.Transfer

To perform server-side redirection, users can use Server.Transfer.  As the execution is transferred on the server, Server.Transfer does not require the client to request another page.  In Server.Transfer, by using HttpContext we can access the source page’s items collection in target page.  The drawback of using this method is that the browser does not know that a different page was returned to it.  It displays the first page's URL in the browser's address bar.  This can confuse the user and cause problems if the user tries to bookmark the page.  Transfer is not recommended since the operations typically flow through several different pages.

·         Others

We can use cache to store data and that can be accessed anywhere from the application.  However, this is recommended only for data that is modified infrequently, but used often.  We can use application variable for some specific purpose, for example, to maintain hit counter in the page.

PreviousPage Property

One improvement in ASP.NET 2.0 regarding Server.Transfer is a new property on the Page called PreviousPage that basically provides a reference to the source page.  When a cross page request occurs, the PreviousPage property of the current Page class holds a reference to the page that caused the postback.  If the page is not the target of a cross-page posting or if the pages are in different applications, the PreviousPage property is not initialized.  After cross-posts back from source page to the target page, the target page accesses information on the source page.  Note that cross page posting is basically a client side transfer.

 

Different ways of Cross Page Posting

·         Using FindControl Method

There are couple of ways of getting control values of the source page into the target page.

Listing 1

<form id="form1" runat="server">
  <div>
    <asp:Label ID="lblUName" runat="server" Text="Name"></asp:Label>
    <asp:TextBox ID="txtUName" runat="server"></asp:TextBox>
    <br />
     <asp:Label ID="lblPWD" runat="server" Text="Password"></asp:Label>
     <asp:TextBox ID="txtPWD" runat="server"></asp:TextBox>
    <br />
    <asp:Button ID="btnGo" runat="server" OnClick="btnGo_Click" Text="GO"      
    PostBackUrl="crosspage2.aspx" />
  </div>
</form>

The above code is for crosspage1.aspx where PostBackUrl property is set for a button named btnGo.  This property takes a string value which points to the location of the file to which this page should post.  In this case, it is page crosspage2.aspx.  This means that the crosspage2.aspx now receives the postback and all the values contained in crosspage1.aspx page controls.  In Listing 3 the TextBox control is created and populated using PreviousPage.FindControl property.  The value of the controls in crosspage1.aspx has been displayed.  Another way of exposing the control values from the crosspage1 to crosspage2 is to create a property of control/controls.

·         Using Control Property

Listing 2

public string Name
{
  get
  {
    return txtUName.Text;
  }
 
}
 
public string Password
{
  get
  {
    return txtPWD.Text;
  }
}

In order to work with the properties described in crosspage1, PreviousPageType directive is required in the posting page that is in crosspage2.aspx. (see code specified in Listing 4).

Listing 3

Response.Write("Name:" + ((TextBox)(PreviousPage.FindControl("txtUName"))).Text   
+ "<BR>");
Response.Write("Password:" +((TextBox)(PreviousPage.FindControl("txtPWD"))).Text    
+ "<BR>");
Response.Write("Name:" + PreviousPage.Name + "<BR>");
Response.Write("Password:" + PreviousPage.Password);

Listing 4

<%@ PreviousPageType VirtualPath="crosspage1.aspx"%>

The above directive points to crosspage1.aspx by specifying the VirtualPath attribute and when that is in place, one can see the properties exposed by crosspage1 in crosspage2 using the intellisence of PreviousPage property.  Note that it is recommended to expose only the information we need as public properties to reduce the amount of information available to potentially malicious users.

IsPostBack and IsCrossPagePostBack

A postback of a page means posting back to the same page.  We can differentiate between the page’s first request and any postbacks by using the Page.IsPostback property which is specified in Listing 5.

Listing 5

if (Page.IsPostBack == true)
{
//Do some processing.
}

Instead of checking against a true or false value, we can also find out that the request is not a postback as the code specified in Listing 6.

Listing 6

if (!Page.IsPostBack)
{
//Do some processing.
}

IsCrossPostBack property, which is new in ASP.NET 2.0, enables us to check whether the request is from a particular page or not.  If any end user request an application page (which can be accessed by registered user only) without login, we can take advantage of IsCrossPostBack property.  Using IsCrossPostBack property, we can check whether the request is coming from a particular page (say crosspage1.aspx page) or not and act accordingly.  If we can proceed that means we can display the requesting page or return to a particular page (say crosspage1.aspx page).  (See the code as specified in Listing 7.)

Listing 7

if(PreviousPage!=null && PreviousPage.IsCrossPagePostBack && PreviousPage.IsValid)
 {
Response.Write("Name:" + 
((TextBox)(PreviousPage.FindControl("txtUName"))).Text + "<BR>");
     Response.Write("Password:" + 
((TextBox)(PreviousPage.FindControl("txtPWD"))).Text + "<BR>");
Response.Write("Name:" + PreviousPage.Name + "<BR>");
Response.Write("Password:" + PreviousPage.Password);
 }
else
 {
Response.Redirect("crosspage1.aspx");
 }
Validation

If validation is working on the client, then the validation will prevent a post back until the user passes all the client side validation tests.  However, we cannot always trust validation on the client side.  We must check the IsValid property of our previous page to make sure the page passed all validation tests (see code specified in Listing 7).

Suggested Readings
Conclusion

We can use the Server.Transfer method to move between pages, however, the URL does not change.  The cross page posting feature in ASP.NET 2.0 allows us to do a normal postback to a different page in the application.  We can then access the values of server controls of the source page in the target page.  Using the cross page posting feature we can also post back to any page and pages in another application.  However, the PreviousPage property will return null if we are posting pages to another application.

 

 

 

 

 


Product Spotlight
Product Spotlight 

©Copyright 1998-2018 ASPAlliance.com  |  Page Processed at 2018-10-21 6:22:09 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search