Let us demonstrate above explanation practically by creating one simple web application are as follows
- Start" - "All Programs" - "Microsoft Visual Studio 2010".
- "File" - "New Project" - "C#" - "Empty Web site " (to avoid adding a master page).
- Provide the project a name such as ResponseTrueAndFalse or another as you wish and specify the location.
- Then right-click on Solution Explorer and select "Add New Item" then select Default.aspx page.
- Add another page and rename it as GetData.aspx.
- 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:
- <%@ 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)
- {
-
- Session["BeforeMsg"] = "Code written Before page Redirecting";
-
-
- Response.Redirect("GetData.aspx",true);
-
-
- 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)
- {
-
- Response.Write(Session["BeforeMsg"] + "</br>");
-
-
- 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)
- {
-
- Session["BeforeMsg"] = "Code written Before page Redirecting";
-
-
- Response.Redirect("GetData.aspx",true);
-
-
- 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)
- {
-
- Response.Write(Session["BeforeMsg"] + "</br>");
-
-
- Response.Write(Convert.ToString(Session["after"]));
-
-
- }
- }
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
- protected void Button1_Click(object sender, EventArgs e)
- {
-
- Session["BeforeMsg"] = "Code written Before page Redirecting";
-
-
- Response.Redirect("GetData.aspx",false);
-
-
- 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.