Articles → .NET → Difference Between Response.Redirect And Server.Transfer

Difference Between Response.Redirect And Server.Transfer











  1. Visual Studio 2008 or Visual Studio 2005 installed on your computer.
  2. Hands on experience on creating a web application.





Creating a web application




  1. Create ASP.NET web application with C# as programming language with name WebApp1.
  2. Add 2 pages page1.aspx and page2.aspx
  3. On page1.aspx add a textbox (TextBox1) and 2 button controls i.e. Button1 and Button2.



Difference


  1. 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.




  2. Response.Redirect(“http://www.gyansangrah.com”);




    Server.Transfer(“http://www.gyansangrah.com”);




    EventOutput
    "Button 1" clickPage is navigated to http://www.gyansangrah.com.
    "Button 2" clickInvalid path for child request 'http://www.gyansangrah.com'. A virtual path is expected.




    Server.Transfer(“Page2.aspx”);






  3. Server.Transfer maintains the original URL in the browser.


  4. EventURL
    "Button 1" clickhttp://localhost/WebApp1/Page2.aspx
    "Button 2" clickhttp://localhost/WebApp1/Page1.aspx




  5. Server.Transfer saves server resources.




  6. Response.Write("Testing123")
    Response.Redirect("Page2.aspx")
    Response.Write("Testing444")




    Response.Write("Testing123")
    Server.Transfer("Page2.aspx")
    Response.Write("Testing444")




    EventOutput
    "Button 1" clickPage is redirected to Page2.aspx and on the page Testing444 is written
    "Button 2" clickURL doesn’t change but page is redirected to Page2.aspx and on the page Testing123 and Testing444 is written




  7. You can access the contents of previous page using Server.Transfer, but not in case of Response.Redirect.




  8. 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)






  9. If PreserveForm Paramater in Server.Transfer is set to true then you can access the values of controls by using Request.Form




  10. Server.Transfer("Page2.aspx",True)




    Response.Write(Request.Form[“TextBox1”]);






    Server.Transfer("Page2.aspx",False)











Posted By  -  Karan Gupta
 
Posted On  -  Tuesday, May 11, 2010

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250