What is the difference between Server.Transfer and
Response.Redirect?

Answer Posted / swati

Response.Redirect simply sends a message down to the
browser, telling it to move to another page. So, you may
run code like:

Response.Redirect("WebForm2.aspx")
or

Response.Redirect("http://www.karlmoore.com/")
to send the user to another page.

Server.Transfer is similar in that it sends the user to
another page with a statement such as Server.Transfer
("WebForm2.aspx"). However, the statement has a number of
distinct advantages and disadvantages.

Firstly, transferring to another page using Server.Transfer
conserves server resources. Instead of telling the browser
to redirect, it simply changes the "focus" on the Web
server and transfers the request. This means you don't get
quite as many HTTP requests coming through, which therefore
eases the pressure on your Web server and makes your
applications run faster.

But watch out: because the "transfer" process can work on
only those sites running on the server, you can't use
Server.Transfer to send the user to an external site. Only
Response.Redirect can do that.

Secondly, Server.Transfer maintains the original URL in the
browser. This can really help streamline data entry
techniques, although it may make for confusion when
debugging.

That's not all: The Server.Transfer method also has a
second parameter—"preserveForm". If you set this to True,
using a statement such as Server.Transfer("WebForm2.aspx",
True), the existing query string and any form variables
will still be available to the page you are transferring to.

For example, if your WebForm1.aspx has a TextBox control
called TextBox1 and you transferred to WebForm2.aspx with
the preserveForm parameter set to True, you'd be able to
retrieve the value of the original page TextBox control by
referencing Request.Form("TextBox1").

This technique is great for wizard-style input forms split
over multiple pages. But there's another thing you'll want
to watch out for when using the preserveForm parameter.
ASP.NET has a bug whereby, in certain situations, an error
will occur when attempting to transfer the form and query
string values. You'll find this documented at
http://support.microsoft.com/default.aspx?id=kb;en-
us;Q316920.

The unofficial solution is to set the enableViewStateMac
property to True on the page you'll be transferring to,
then set it back to False. This records that you want a
definitive False value for this property and resolves the
bug.

So, in brief: Response.Redirect simply tells the browser to
visit another page. Server.Transfer helps reduce server
requests, keeps the URL the same and, with a little bug-
bashing, allows you to transfer the query string and form
variables.

Is This Answer Correct ?    4 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is the difference between equals() and == in c#?

654


Explain how can we inherit a static member?

611


What are user controls?

660


Is a dll file an executable?

635


What does session_start () do?

731






What does postback mean?

623


Do I need to have the latest version of windows media player installed?

596


How dataadapter.fill works?

653


What is cross page posting in asp net?

612


If you want to bind the columns manually within the asp:datagrid tags what kind of tags you have to add.

737


Where sessions are stored in asp.net?

616


What is the difference between union and join?

649


What is the use of service provider?

656


what is command line compiler.what are the steps and how it is related to debugging.

1498


Difference between .NET and previous version?

2208