Articles → .NET → Difference Response.Redirect and Server.Transfer

Difference Response.Redirect and Server.Transfer




















Creating a web application
















•  On the Button1 click event write the following code

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


•  On the Button2 click event write the following code

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


•  Run the application

•  Click on the Button1.















•  Change the code in Button2 click to

Server.Transfer(“Page2.aspx”);


•  Run the application

•  Click Button2.











•  Run the application

•  Click on the Button1.

•  Check the URL in the browser.







•  Go back to page Page1.aspx

•  Click on the Button2

•  Check the URL in the browser.













•  On the click of Button1 add the following code

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


•  On the click of Button2 add the following code

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


•  Run the application.

•  Click Button1. You will observe that page is redirected to Page2.aspx and on the page Testing444 is written.

•  Now go back to previous page again and click Button2 .As mentioned in the point2 URL doesn’t change but page is redirected to Page2.aspx and on the page Testing123 and Testing444 is written.









•  Write following code on Button1 click

Response.Redirect("Page2.aspx")


•  Write following code on Button2 click

Server.Transfer("Page2.aspx")


•  On page load of Page2.aspx write following code. This code is to access the content of previous page.

Dim poster As Page = Me.PreviousPage
Dim txtTest As TextBox = CType(poster.FindControl("TextBox1"), TextBox)
Response.Write(txtTest.Text)


•  Run the application.

•  Add some text in the textbox.

•  Click Button1. Page is redirected to Page2.aspx but you will get an error because in case of Response.Redirect Me.PreviousPage is null.

•  Now go back to previous page again

•  Add some text in the textbox.

•  Click Button2







•  On Click on Button2 write following code

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


•  On page load of Page2.aspx add the following code

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


•  Run the application.

•  Add some text in TextBox.

•  Click Button1.

•  Page is navigated to Page2.aspx and you will get an error -

•  Now go back to Page1.aspx.

•  Enter some text in the textbox and click button2.

•  Page is navigated to Page2.aspx and you can see the text of Textbox written on the page load of Page2.aspx.

•  Change the code of Button2 to

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




•  Again run the application.

•  Enter some text in the textbox and click Button2

•  Page is navigated to Page2.aspx and you will get an error -





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

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250