Both are the objects of ASP.NET. 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:
- 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.
- 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.
- 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.
- 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.
Description in Detail with Example:
Suppose you are currently on the Default1.aspx and now you are transferring the user to the Default2.aspx using Response.Redirect then When the Default2 page is requested, Default1 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 Default2 can read properties directly from Default1 because it’s still in memory, as you know the Server.Transfer just changes the focus from Default1 to Default2 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.
If you want to pass state from the source page to the new page, you have to pass it either on the URL (such as a database key, or message string), or you can store it in the Session object (caveat: there may be more than one browser window, and they’ll all use the same session object).
e.g. Redirect to the Default1.aspx page, passing an ID on the query string. "true" stops processing the current page:
e.g. Redirect to the Default1.aspx page, passing an ID on the query string. "true" stops processing the current page:
protected void Button2_Click(object sender, EventArgs e) { Response.Redirect("Default2.aspx?id=34", true); } |
Now fetching value of id on Default2.aspx Page, type following:
protected void Page_Load(object sender, EventArgs e) { Response.Write(Request.QueryString["id"].ToString()); } |
Passing TextBox Value from Default1.aspx to Default2.aspx by using Response.Transfer
Create Controls on Default1.aspx form like this:
<html> <body> <form> <div> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:Button ID="btn_Submit" runat="server" onclick="btn_Submit _Click" Text="Submit" /> </div> </form> </body> </html> |
Coding in .cs Class:
protected void btn_Submit_Click(object sender, EventArgs e) { Server.Transfer("Default2.aspx"); } |
Coding for Default2.aspx Page:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Page originalPage = (Page)Context.Handler; TextBox textBox1 = (TextBox)originalPage.FindControl("TextBox1"); Response.Write(textBox1.Text); } } |
Note: Now run the project, type “Welcome ASP.net” in TextBox1 of Default1.aspx Page. Now click on Submit Button.
After this, Default2.aspx Page will open with text “Welcome ASP.net” on Screen.