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.
    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.
    Response.Redirect("Default.aspx", false);  
    Response.Redirect("Default.aspx", true); 

What is the 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 look like as follows

Now the Default.aspx source code will be as follows:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head id="Head1" runat="server">  
    <title>Article by Vithal Wadje</title>  
</head>  
<body bgcolor="blue">  
    <form id="form2" runat="server">  
    <h4 style="color: White;">  
        Article for C#Corner  
    </h4>  
    <div>  
    </div>  
    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Redirect" />  
    </form>  
</body>  
</html>

Now open the default.aspx page and write the following code on button click event are as

protected void Button1_Click(object sender, EventArgs e)  
{  
    //store string in session which will be accessed on next page  
    Session["BeforeMsg"] = "Code written Before page Redirecting";  
  
    //Redirectig page  
    Response.Redirect("GetData.aspx",true);  
  
    //store string in session which will be accessed on next page  
    Session["after"] = "code written after page Redirecting";  
}

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

protected void Page_Load(object sender, EventArgs e)  
{  
    //accessing the session value which is written before redirecting page  
    Response.Write(Session["BeforeMsg"] + "</br>");  
  
    //accessing the session value which is written after redirecting page  
    Response.Write(Convert.ToString(Session["after"]));     
}

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

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Web;  
using System.Web.UI;  
using System.Web.UI.WebControls;  
using System.Text;  
public partial class _Default : System.Web.UI.Page  
{  
    protected void Page_Load(object sender, EventArgs e)  
    {  
  
    }  
    protected void Button1_Click(object sender, EventArgs e)  
    {  
        //store string in session which will be accessed on next page  
        Session["BeforeMsg"] = "Code written Before page Redirecting";  
  
        //Redirectig page  
        Response.Redirect("GetData.aspx",true);  
  
        //store string in session which will be accessed on next page  
        Session["after"] = "code written after page Redirecting";  
    }  
}

GetData,aspx.cs

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Web;  
using System.Web.UI;  
using System.Web.UI.WebControls;  
  
public partial class GetData : System.Web.UI.Page  
{  
    protected void Page_Load(object sender, EventArgs e)  
    {  
        //accessing the session value which is written before redirecting page  
        Response.Write(Session["BeforeMsg"] + "</br>");  
  
        //accessing the session value which is written after redirecting page  
        Response.Write(Convert.ToString(Session["after"]));         
    }  
}

Now run the application then the page will 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

protected void Button1_Click(object sender, EventArgs e)  
{  
    //store string in session which will be accessed on next page  
    Session["BeforeMsg"] = "Code written Before page Redirecting";  
  
    //Redirectig page  
    Response.Redirect("GetData.aspx",false);  
  
    //store string in session which will be accessed on next page  
    Session["after"] = "code written after page Redirecting";
}

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.


Similar Articles