Response.Redirect true and Response.Redirect False in Asp.Net

Background
 
Many times there is need to Execute the line in the program which are written after the redirecting the page and also this is frequently asked question in interview that is what is difference between Response.Redirect true and Response.Redirect false, so by considering above requirement I have decided to write this article.
 
What is Response.Redirect true and Response.Redirect false?
 
These are the Response objects of Asp.Net which are used to redirect page from one page to another page and true and false are the optional parameters of the Redirect methods which decides whether the current page response terminate or not.
 
Response.Redirect  method takes the following parameter 
  1. Url (string)
  2. EndResponse  (Boolean)


In the above image you have seen that Redirect method takes the two parameters
  •  Url: This is a string parameter in which url or page name is given that helps to navigate from one page to another page.

Syntax

  1. Response.Redirect("Default.aspx"); 
  •  EndResponse: EndResponse is the optional parameter of the Redirect method having true and false values ,when you false value then its does not terminate the execution of the current page, the default is true.

Syntax

  1. Response.Redirect("Default.aspx"false);  
  2.  Response.Redirect("Default.aspx"true); 
What is difference between Response.Redirect true and false ?
 
When you use  Response.Redirect("Default.aspx", false) then the execution of current page is not terminated instead of this code written after the Response.Redirect is executed and then after that page is redirected to the given page.
 
When you use Response.Redirect("Default.aspx",true  ) which is by default true  then the execution of current page is terminated and code written after the Response.Redirect is not executed instead of executing code written after the Response.Redirect ,the page is redirected to the given page. 
 
Key Points 
  1. Response.Redirect is the method of Aps.net which is used to transfer the page from one page to another.
  2. Response.Redirect method takes two parameter URL and endResponse.
  3. Response.Redirect has URL is the mandatory parameter where as endResponse is optional parameter.
  4. url is the string parameter which takes the url or page name and endResponse is the boolean parameter which has true and false values.
  5. By default Response.Redirect has EndResponse value is true.
  6. Response.Redirect("Default.aspx", false) means current page execution is not terminated and code written after the Response.Redirect("Default.aspx", false) is executed and then after the page is redirected to the Default.aspx.
  7. Response.Redirect("Default.aspx", true) means current page execution is terminated and page is redirected to the default.aspx page without executing the code which is written after the Response.Redirect("Default.aspx", true).

Let us demonstrate above explanation practically by creating one simple web application are as follows

  1. Start" - "All Programs" - "Microsoft Visual Studio 2010".

  2. "File" - "New Project" - "C#" - "Empty Web site " (to avoid adding a master page).

  3. Provide the project a name such as ResponseTrueAndFalse or another as you wish and specify the location.

  4. Then right-click on Solution Explorer and select "Add New Item" then select Default.aspx page. 

  5. Add another page and rename it as GetData.aspx.

  6. Drag and Drop one button onto the Default.aspx page.

Now the solution explorer will be look like as follows

Now the Default.aspx source code will be as follows:
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2.   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4. <html xmlns="http://www.w3.org/1999/xhtml">  
  5. <head id="Head1" runat="server">  
  6.     <title>Article by Vithal Wadje</title>  
  7. </head>  
  8. <body bgcolor="blue">  
  9.     <form id="form2" runat="server">  
  10.     <h4 style="color: White;">  
  11.         Article for C#Corner  
  12.     </h4>  
  13.     <div>  
  14.     </div>  
  15.     <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Redirect" />  
  16.     </form>  
  17. </body>  
  18. </html> 
Now open the default.aspx page and write the following code on button click event are as
  1. protected void Button1_Click(object sender, EventArgs e)  
  2.     {  
  3.         //store string in session which will be accessed on next page  
  4.         Session["BeforeMsg"] = "Code written Before page Redirecting";  
  5.   
  6.         //Redirectig page  
  7.         Response.Redirect("GetData.aspx",true);  
  8.   
  9.         //store string in session which will be accessed on next page  
  10.         Session["after"] = "code written after page Redirecting";  
  11.   
  12.     } 
Code explanation
 
In the above code sample ,we are calling response.Redirect method and before and also after  Response.Redirect method ,we are storing one sting message to the session which will accessed on another page.
The above code will not be execute the code written after the Response.Redirect method because we are ing true values in Response.Redirect method which means terminate the current page execution and redirect to the GetData.aspx page.
 
Now open DetData.aspx.cs Page and write the following code on page load as
  1. protected void Page_Load(object sender, EventArgs e)  
  2.     {  
  3.         //accessing the session value which is written before redirecting page  
  4.         Response.Write(Session["BeforeMsg"] + "</br>");  
  5.   
  6.         //accessing the session value which is written after redirecting page  
  7.         Response.Write(Convert.ToString(Session["after"]));  
  8.   
  9.          
  10.     } 
Code explanation
 
In the above code sample  we are accessing the session values which are stored in session  before and after page redirecting.
 
Whole code of the default.aspx.cs and GetData,aspx.cs  will be as follows :
 
default.aspx.cs  
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Text;  
  8. public partial class _Default : System.Web.UI.Page  
  9. {  
  10.     protected void Page_Load(object sender, EventArgs e)  
  11.     {  
  12.   
  13.     }  
  14.     protected void Button1_Click(object sender, EventArgs e)  
  15.     {  
  16.         //store string in session which will be accessed on next page  
  17.         Session["BeforeMsg"] = "Code written Before page Redirecting";  
  18.   
  19.         //Redirectig page  
  20.         Response.Redirect("GetData.aspx",true);  
  21.   
  22.         //store string in session which will be accessed on next page  
  23.         Session["after"] = "code written after page Redirecting";  
  24.   
  25.     }  

GetData,aspx.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7.   
  8. public partial class GetData : System.Web.UI.Page  
  9. {  
  10.     protected void Page_Load(object sender, EventArgs e)  
  11.     {  
  12.         //accessing the session value which is written before redirecting page  
  13.         Response.Write(Session["BeforeMsg"] + "</br>");  
  14.   
  15.         //accessing the session value which is written after redirecting page  
  16.         Response.Write(Convert.ToString(Session["after"]));  
  17.   
  18.          
  19.     }  

Now run the application then the page will be look like as follows
 
 
 
Now click on above button then the Page will be redirected to the Next page that is GetData.aspx page as
 
 
 
In the above image you have seen that only session value is displayed which is set before the Response.Redirect method because we used true value in Response.Redirect("GetData.aspx",true) method so the current page execution is terminated and redirected to the next page instead of executing code which is written after the Response.Redirect method.
 
Now let us change in code which will execute the code which is written after the Response.Redirect as
  1. protected void Button1_Click(object sender, EventArgs e)  
  2.    {  
  3.        //store string in session which will be accessed on next page  
  4.        Session["BeforeMsg"] = "Code written Before page Redirecting";  
  5.   
  6.        //Redirectig page  
  7.        Response.Redirect("GetData.aspx",false);  
  8.   
  9.        //store string in session which will be accessed on next page  
  10.        Session["after"] = "code written after page Redirecting";  
  11.   
  12.    } 
Now run the application and click on Redirect button, it will shows the following output
 
 
 
In the above image you have seen that both session values are displayed which are set before and after the Response.Redirect method because we used false value in Response.Redirect("GetData.aspx",false) method so the current page execution not terminated and it  execute code which is written after the Response.Redirect method and then redirect the page to the GetData.aspx
 
From all above explanation we have learned the difference between the true and false property and also when to use true and false in the Response.Redirect method.
 
Notes
  • Download the Zip file from the attachment for the full source code of the application.
Summary

I hope this article is useful for all readers, if you have any suggestion then please contact me including beginners also.
 

Up Next
    Ebook Download
    View all
    Learn
    View all