Many times developers get confused with the use of Response.Redirect and Server.Transfer .
Server.Transfer does the same thing as that of Response.Redirect , but it saves server resources by avoiding a round trip to the client. When we use Response.redirect, it tells browser for the new page and browser sends another request for the new URL. So there are actually 2 requests instead of 1, but the benefit is we can redirect to a page on an external site.
In contrast to the above, Server.Transfer only transfers the page at server side and does not make any round trip to client’s browser. In the client side URL remains same as that of old page but with the content of new page. This is sometime confusing for the client and it also creates confusion at the time of debugging.
Apart from being performance oriented, it can also transfer form control with its value to the next page. For that we have to set a second parameter as “true” as following which will preserve the state.
Server.Transfer(“~/Webpage2.aspx” , “true”) . But to make it work properly we need to set the attribute “EnableViewStateMac” to false in page directive.
<%@ Page Language=”C#” EnableViewStateMac=”False”%>. Now we can get all the values from previous page.
Another way to maintain state across pages is using Context.Items collection. We can store context message in one page and retrieve the value in the destination page.
Context.Items[“Message”] = “You have transferred it successfully”;
In the following code I have shown how we are getting previous page value.
<form id="form1" runat="server">
Enter your Name:
<asp:TextBox ID="txtNameField" runat="server"></asp:TextBox>
<asp:Button ID="btnRedirect" runat="server" Text="Redirect"
.cs file :
protected void Page_Load(object sender, EventArgs e)
protected void btnRedirect_Click(object sender, EventArgs e)
Context.Items["Wish"] = "Congratulation !!!";
Context.Items["Msg"] = "Your Name is passed to second page successfully";
Server.Transfer("TransferToSecond.aspx" , true);