Articles → .NET → Difference Between Response.Redirect And Server.Transfer
Difference Between Response.Redirect And Server.Transfer
- Visual Studio 2008 or Visual Studio 2005 installed on your computer.
- Hands on experience on creating a web application.
Creating A Web Application
- Create ASP.NET web application with C# as programming language with name WebApp1.
- Add 2 pages page1.aspx and page2.aspx
- On page1.aspx add a textbox (TextBox1) and 2 button controls i.e. Button1 and Button2.
Difference
- In case of Server.Transfer you cannot send the user to external sites but by using Response.Redirect you can send the user to external site.
Response.Redirect(“http://www.gyansangrah.com”);
Server.Transfer(“http://www.gyansangrah.com”);
Event | Output |
---|
"Button 1" click | Page is navigated to http://www.gyansangrah.com. |
"Button 2" click | Invalid path for child request 'http://www.gyansangrah.com'. A virtual path is expected. |
Server.Transfer(“Page2.aspx”);
- Server.Transfer maintains the original URL in the browser.
Event | URL |
---|
"Button 1" click | http://localhost/WebApp1/Page2.aspx |
"Button 2" click | http://localhost/WebApp1/Page1.aspx |
- Server.Transfer saves server resources.
Response.Write("Testing123")
Response.Redirect("Page2.aspx")
Response.Write("Testing444")
Response.Write("Testing123")
Server.Transfer("Page2.aspx")
Response.Write("Testing444")
Event | Output |
---|
"Button 1" click | Page is redirected to Page2.aspx and on the page Testing444 is written |
"Button 2" click | URL doesn’t change but page is redirected to Page2.aspx and on the page Testing123 and Testing444 is written |
- You can access the contents of previous page using Server.Transfer, but not in case of Response.Redirect.
Response.Redirect("Page2.aspx")
Server.Transfer("Page2.aspx")
Dim poster As Page = Me.PreviousPage
Dim txtTest As TextBox = CType(poster.FindControl("TextBox1"), TextBox)
Response.Write(txtTest.Text)
- If PreserveForm Paramater in Server.Transfer is set to true then you can access the values of controls by using Request.Form
Server.Transfer("Page2.aspx",True)
Response.Write(Request.Form[“TextBox1”]);
Server.Transfer("Page2.aspx",False)