Response.Redirect, the
redirection happens on the client browser
, while Server.Transfer simply
change focus between the pages
in the same application. Response.Redirect
postback and the life cycle of the redirecting page ends
. Server.Transfer still keeps the previous page in memory and can be accessed through the
previous page
property.
Here is an example shows Server.Transfer still keeps the previous page in memory.
Have a simple interface
public interface IMarker
{
string TestString { get; set; }
}
At page1 implement this interface and set the value for the test string. At a button click event handler it transfers the page.
public partial class _Default : System.Web.UI.Page,IMarker
{
protected void Page_Load(object sender, EventArgs e)
{
TestString = "Hello world";
}
public string TestString { get; set; }
protected void Button1_Click(object sender, EventArgs e)
{
Server.Transfer("~/Default2.aspx");
}
}
In the page 2 i.e Default2.aspx you can access this property which is still resides in memory.
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
IMarker page = (IMarker)this.PreviousPage;
Response.Write(page.TestString);
}
}
Hope this explains how server tranfer works. Response redirect has to be used when simple redirection needs or redirect to a url which is not belong to this application. Response.Redirect goes a round trip page 1 to browser to page 2, where as transfer straightly move from page 1 to page to from the server side itself, but it still holds the page1 in memory. So choice is yours according to your requirement.
Good luck