Introduction
This article tell you the difference between Server.Transfer and Response.Redirect.
Server.Transfer v/s Response.Redirect
Both "Server" and "Response" are objects of ASP.NET. Server.Transfer and Response.Redirect both are used to transfer a user from one page to another page. Both are used for the same purpose but still there are some differences are there between both that are as follows:
• Syntactically both are different, if you want to transfer the user to a page named newpage.aspx then syntax for both methods will be, Response.Redirect(“newpage.aspx”) and Server.Transfer(“newpage.aspx”)
• Response.Redirect involves a roundtrip to the server whereas Server.Transfer conserves server resources by avoiding the roundtrip. It just changes the focus of the webserver to a different page and transfers the page processing to a different page.
Roundtrip means in case of Response.Redirect it first sends the request for the new page to the browser then browser sends the request for the new page to the webserver then after your page changes But in case of Server.Transfer it directly communicate with the server to change the page hence it saves a roundtrip in the whole process.
• If you are using Server.Transfer then you can directly access the values, controls and properties of the previous page which you can’t do with Response.Redirect.
Suppose you are currently on the Page1.aspx and now you are transferring the user to the Page2.aspx using Response.Redirect then When the Page2 page is requested, Page1 has been flushed from the server’s memory and no information can be retrieved about it unless the developer explicitly saved the information using some technique like session, cookie, application, cache etc. But in case of Server.Transfer variables can stay in scope and Page2 can read properties directly from Page1 because it’s still in memory, as you know the Server.Transfer just changes the focus from page1 to page2 So in this case browser doesn’t know that any change is happen there that’s why with this method you can access the information about the previous page.
• Response.Redirect changes the URL in the browser’s address bar. So they can be bookmarked. Whereas Server.Transfer retains the original URL in the browser’s address bar. It just replaces the contents of the previous page with the new one.
Actually in case of Server.Transfer it directly contact with the webserver for the new page request, it doesn’t involve the browser, so that browser doesn’t know that there is any change happen. But in case of Response.Redirect as you know it first send the request (for new page) to the browser then further processing will be performed, so here browser knows that yes there is some change in the browser window that’s why it changes the URL in the address bar.
So, the matter “No change of address in the browser address bar” This is a good thing if you see it from security point of view but it creates problem in case if you refresh your page or in case you want to add bookmark of that page. In case of refresh and bookmark it will add perform both the action with the URL currently present in the address bar, but as you know Server.Transfer doesn’t changes the URL, so sometimes it creates problem.
• Response.Redirect can be used for both .aspx and html pages whereas Server.Transfer can be used only for .aspx pages and is specific to ASP and ASP.NET.
With Response.Redirect you can redirect the user to the both type of pages .html or .aspx like below, Response.Redirect(“mypage.html”) OR Response.Redirect(“OtherPage.aspx”) But in case of Server.Transfer you can only work with .asp or .aspx page like below Server.Transfer(“mypage.asp”) OR Server.Transfer(“OtherPage.aspx”)
• Response.Redirect can be used to redirect a user to an external websites. Server.Transfer can be used only on sites running on the same server. You cannot use Server.Transfer to redirect the user to a page running on a different server.
Suppose on some action on the webpage I want to redirect my user to the http://www.yahoo.com so with Response.Redirect you can redirect your user to the external site, but in case of Server.Transfer you can only work with the .asp or .aspx pages that are present in your site.
Now the question is which to use and when to use? Mostly the Server.Transfer method is preferable to use because Server.Transfer is faster since there is one less roundtrip, but in some people say that Server.Transfer is not recommended since the operations typically flow through several different pages due to which you lose the correct URL of the page, but again all depends on your requirement.
Server.Transfer also allows for more flexibility since you can use HTTPContext.Items to pass variables between pages so, use Server.Transfer when you need to pass context items. Otherwise use Response.Redirect so the user will always see the correct URL in the address bar.
I mentioned the most common differences here if anyone knows any other difference other than these then please give your comments.
Thank you,
Wednesday, February 18, 2009
Monday, February 9, 2009
Cross Page posting in ASP.NET 2.0
SourcePage which does Cross page post back to Destination page
Fig (1.2) Please Refer Fig 1.1 (i.e SourcePage which does Cross page post back to Destination page)
You can see in below code how we are able to retrieve values from previous page using FindControl method and display them on Destination Page. Below given output should help you understand the funda.
Protected Sub form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles form1.LoadLabel1.Text = “Name:” + CType(PreviousPage.FindControl(“txtName”), TextBox).TextLabel2.Text = “Password:” + CType(PreviousPage.FindControl(“txtPassword”), TextBox).TextEnd Sub
source: http://www.beansoftware.com
Fig (1.2) Output listing
We always tend to post data from one page to another in a typical web application. For example, user name entered on login page getting displayed in welcome message on homepage.
How do you generally post values/data from one page to another page in ASP.NET 1.x? There are different ways in which you can exchange the data in ASP.NET 1.x like Query strings, Server.Transfer method, Response.Redirect method and session variables. All these techniques have their own merits and demerits like browser imposed character limit when passing parameters using Query Strings and indiscriminate usage of session variables can prove costly in terms of load on server and eventually impacts the performance of server.
Keeping in view above limitations in ASP.NET 1.x, Microsoft has reinstated Cross Page posting feature in ASP.NET 2.0. Many of us might not be aware of Cross Page posting feature in classic ASP. Cross Page posting feature allows a page to cross post to another page which in turn allows you to access all data in posted source page.
In ASP.NET 1.x , when you use look at page life level, you observe that a page can only submit to itself. Cross Page posting feature allows us post the form along with all its control values into another page.
Any server control which implements System.Web.UI.WebControls.IButtonControl interface can be used for Cross page posting. Examples of such controls include link button, Image button and submit button
By simply setting PostbackUrl property, we specify the destination page to which present page should post when post back occurs on present page.
Different methods to post data
There are several methods to post data from one page to another page depending on your needs. If you need to exchange simple insensitive data, you can give a try with existing techniques. So, let us revisit those different techniques which allow exchanging data from one page to another.
Response.Redirect method
We can use this method to allow client browser to do redirection from one page to another page. This technique is usually referred to as Client side redirection. When client browser redirects to DestinationPage.aspx, it involves Server sending a HTTP header informing that the user should redirect and the Client requests the DestinationPage.aspx. Server sends DestinationPage.aspx. Now the Client’s address bar shows DestinationPage.aspx. This entire series of steps incurs extra round trip to server which hits the performance. We usually associate Query Strings with Response.Redirect. There is always a client browser imposed limitation on the length of a query string; so you end up sending small amounts of data over the wire.
Session Variables
We can use session variable to store page information and use it across different pages for entire user session. However, this involves of consumption of costly server resources. This approach may prove disastrous when large numbers of users connect to server and things go pretty poor.
Server.Transfer Method
On flip side to Client side redirection, we have server doing the page transfer. In Server.Transfer, Http Context is preserved when moving from one page to another which means 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 still displays the previous source page’s URL in the address bar. This can confuse the user. Transfer is not recommended since the operations typically flow through several different pages.
Technically speaking, Server.Transfer is faster since there is one less roundtrip, but you lose the URL of the page. Server.Transfer also allows for more flexibilty since you can use HTTPContext.Items to pass variables between pages.
Use Server.Transfer when you need to pass context items. Otherwise use Response.Redirect so the user will always see the correct URL in the address bar.
New Properties helping in Cross Page posting
The new PreviousPage property provides 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 post back. 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 using this property.
Different ways of implementing Cross Page Posting
There are couples of ways of getting control values of the source page into the target page. First way being using FindControl method as discussed below
Using FindControl Method
In the below listing, we set the PostBackUrl property for Submit button. This property points to the location of the file to which this page should post. In this case, it is DestinationPage.aspx. This means that the DestinationPage.aspx receives the postback and all the values contained in CrossPostingPage.aspx page controls as shown below
How do you generally post values/data from one page to another page in ASP.NET 1.x? There are different ways in which you can exchange the data in ASP.NET 1.x like Query strings, Server.Transfer method, Response.Redirect method and session variables. All these techniques have their own merits and demerits like browser imposed character limit when passing parameters using Query Strings and indiscriminate usage of session variables can prove costly in terms of load on server and eventually impacts the performance of server.
Keeping in view above limitations in ASP.NET 1.x, Microsoft has reinstated Cross Page posting feature in ASP.NET 2.0. Many of us might not be aware of Cross Page posting feature in classic ASP. Cross Page posting feature allows a page to cross post to another page which in turn allows you to access all data in posted source page.
In ASP.NET 1.x , when you use look at page life level, you observe that a page can only submit to itself. Cross Page posting feature allows us post the form along with all its control values into another page.
Any server control which implements System.Web.UI.WebControls.IButtonControl interface can be used for Cross page posting. Examples of such controls include link button, Image button and submit button
By simply setting PostbackUrl property, we specify the destination page to which present page should post when post back occurs on present page.
Different methods to post data
There are several methods to post data from one page to another page depending on your needs. If you need to exchange simple insensitive data, you can give a try with existing techniques. So, let us revisit those different techniques which allow exchanging data from one page to another.
Response.Redirect method
We can use this method to allow client browser to do redirection from one page to another page. This technique is usually referred to as Client side redirection. When client browser redirects to DestinationPage.aspx, it involves Server sending a HTTP header informing that the user should redirect and the Client requests the DestinationPage.aspx. Server sends DestinationPage.aspx. Now the Client’s address bar shows DestinationPage.aspx. This entire series of steps incurs extra round trip to server which hits the performance. We usually associate Query Strings with Response.Redirect. There is always a client browser imposed limitation on the length of a query string; so you end up sending small amounts of data over the wire.
Session Variables
We can use session variable to store page information and use it across different pages for entire user session. However, this involves of consumption of costly server resources. This approach may prove disastrous when large numbers of users connect to server and things go pretty poor.
Server.Transfer Method
On flip side to Client side redirection, we have server doing the page transfer. In Server.Transfer, Http Context is preserved when moving from one page to another which means 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 still displays the previous source page’s URL in the address bar. This can confuse the user. Transfer is not recommended since the operations typically flow through several different pages.
Technically speaking, Server.Transfer is faster since there is one less roundtrip, but you lose the URL of the page. Server.Transfer also allows for more flexibilty since you can use HTTPContext.Items to pass variables between pages.
Use Server.Transfer when you need to pass context items. Otherwise use Response.Redirect so the user will always see the correct URL in the address bar.
New Properties helping in Cross Page posting
The new PreviousPage property provides 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 post back. 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 using this property.
Different ways of implementing Cross Page Posting
There are couples of ways of getting control values of the source page into the target page. First way being using FindControl method as discussed below
Using FindControl Method
In the below listing, we set the PostBackUrl property for Submit button. This property points to the location of the file to which this page should post. In this case, it is DestinationPage.aspx. This means that the DestinationPage.aspx receives the postback and all the values contained in CrossPostingPage.aspx page controls as shown below
You can see in below code how we are able to retrieve values from previous page using FindControl method and display them on Destination Page. Below given output should help you understand the funda.
Protected Sub form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles form1.LoadLabel1.Text = “Name:” + CType(PreviousPage.FindControl(“txtName”), TextBox).TextLabel2.Text = “Password:” + CType(PreviousPage.FindControl(“txtPassword”), TextBox).TextEnd Sub
Please refer Fig 1.2 (i.e Output listing)
Using Control Property
Another way of exposing the control values from the Source Page to Destination Page is to create a public property of controls as shown below
Public Property UserName() As StringGetReturn UserNameEnd GetSet(ByVal value As String)End SetEnd PropertyPublic Property Password() As StringGetReturn PasswordEnd GetSet(ByVal value As String)End SetEnd Property
In order to work with the properties described in FindControl page as shown above, PreviousPageType directive should be included in the destination page that is in DestinationPage.aspx. See below listing
<%@ PreviousPageType VirtualPath=”FindControl.aspx”%>
The above directive points to FindControl.aspx by specifying the VirtualPath attribute and when that is in place, one can see the properties exposed by FindControl page in DestinationPage using the PreviousPage property. It is always better to expose only the information we need as public properties to reduce the amount of information available to potentially malicious users.
IsCrossPagePostBack Property
IsCrossPostBack property, which is new in ASP.NET 2.0, enables us to check whether the page is participating in a cross page request. Using IsCrossPostBack property, we can check whether the request is coming from a particular page or not and act accordingly. As per above example, DestinationPage can include this property to specially process the FindControl.aspx cross page post back. Similarly, we can use the IsValid property of our previous page to make sure the page passed all client validations before it is cross posted to DestinationPage.
If Page.IsCrossPagePostBack ThenLabel1.Text = “Name:” + CType(PreviousPage.FindControl(“txtName”), TextBox).TextLabel2.Text = “Password:” + CType(PreviousPage.FindControl(“txtPassword”), TextBox).TextElseLabel1.Text = “No Name:Normal Postback”Label2.Text = “No password: normal postback”End If
Hence, Cross Page posting is very handy feature to implement very specific scenarios like displaying logged in user details on different pages and ensuring the request is coming from correct page. You can implement this feature in either two ways using FindControl method or exposing values as public properties. PreviousPage object will expose very useful properties like IsValid property and FindControl property to make Cross Page Posting more useful.
Using Control Property
Another way of exposing the control values from the Source Page to Destination Page is to create a public property of controls as shown below
Public Property UserName() As StringGetReturn UserNameEnd GetSet(ByVal value As String)End SetEnd PropertyPublic Property Password() As StringGetReturn PasswordEnd GetSet(ByVal value As String)End SetEnd Property
In order to work with the properties described in FindControl page as shown above, PreviousPageType directive should be included in the destination page that is in DestinationPage.aspx. See below listing
<%@ PreviousPageType VirtualPath=”FindControl.aspx”%>
The above directive points to FindControl.aspx by specifying the VirtualPath attribute and when that is in place, one can see the properties exposed by FindControl page in DestinationPage using the PreviousPage property. It is always better to expose only the information we need as public properties to reduce the amount of information available to potentially malicious users.
IsCrossPagePostBack Property
IsCrossPostBack property, which is new in ASP.NET 2.0, enables us to check whether the page is participating in a cross page request. Using IsCrossPostBack property, we can check whether the request is coming from a particular page or not and act accordingly. As per above example, DestinationPage can include this property to specially process the FindControl.aspx cross page post back. Similarly, we can use the IsValid property of our previous page to make sure the page passed all client validations before it is cross posted to DestinationPage.
If Page.IsCrossPagePostBack ThenLabel1.Text = “Name:” + CType(PreviousPage.FindControl(“txtName”), TextBox).TextLabel2.Text = “Password:” + CType(PreviousPage.FindControl(“txtPassword”), TextBox).TextElseLabel1.Text = “No Name:Normal Postback”Label2.Text = “No password: normal postback”End If
Hence, Cross Page posting is very handy feature to implement very specific scenarios like displaying logged in user details on different pages and ensuring the request is coming from correct page. You can implement this feature in either two ways using FindControl method or exposing values as public properties. PreviousPage object will expose very useful properties like IsValid property and FindControl property to make Cross Page Posting more useful.
source: http://www.beansoftware.com
Subscribe to:
Comments (Atom)
