Response.Redirect in ASP.NET and use of EndResponse


Response.Redirect in ASP.NET


In the previous part we study about what is page navigation and there types and also study HyperLink type page navigation technique in detail. In this part we will study about Response.Redirect which also one of the navigation techniques in ASP.NET which is use to move from one web form to another that means use to redirect a user to another URL.

According to MSDN website it redirect to new URL whether execution of the current page terminated.

Redirect is a method of the HttpResponse class in ASP.NET applications, when hover mouse on it Response class and return type is void.

The HttpResponse class has two overloaded method of versions of the Redirect.

The first method takes one input parameter as a URL of the other page.
e.g.  Response.Redirect("~/default.aspx");

response.redirect c# true false


Another one is take two parameters, one as a URL and second is a Boolean value (true or false).
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.
or
Response.Redirect("~/default.aspx",true); 
When we try to use Response.Redirect("default.aspx", false) in a page 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 we try to 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.

Note:-

1. When you use the first overloaded version in the code, the second overloaded version is called automatically and endResponse passed True .

2. When we pass false as second parameter value that indicates we do not want an exception to be thrown to terminate the page. 3. When we use overloaded method, URL (string type) is mandatory parameter and endResponse (Boolean type, by default true) 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.

We create z-test.aspx page in ASP.NET and drag and drop buttons control in this page from toolbox.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="z-test.aspx.cs" Inherits="AspDotNets.z_test" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
<asp:Button ID="button1" runat="server" Text="Button 1" OnClick="button1_Click" /><br />
<asp:Button ID="button2" runat="server" Text="Button 2" OnClick="button2_Click"/><br /> 
 <asp:Button ID="button3" runat="server" Text="Button 3" OnClick="button3_Click" /><br />
<asp:Button ID="button4" runat="server" Text="Button 4" OnClick="button4_Click" /><br />
</form>
</body>
</html>
Now write the code in code behind (z_test.aspx.cs).
using System;
namespace AspDotNets
{
    public partial class z_test : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }
        protected void button1_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/test2.aspx");
        }
        protected void button2_Click(object sender, EventArgs e)
        {
            Session["Before_redirect"] = "This String is Before Response.Redirect When EndResponse values is false"; 
            Response.Redirect("~/z-test2.aspx",false);
            Session["After_redirect"] = "This String is after Response.Redirect When EndResponse values is false"; 

        }
        protected void button3_Click(object sender, EventArgs e)
        {
            Session["Before_redirect"] = "This String is Before Response.Redirect When EndResponse values is true"; 
            Response.Redirect("~/z-test2.aspx",true);
            Session["After_redirect"] = "This String is after Response.Redirect When EndResponse values is true"; 
        }
        protected void button4_Click(object sender, EventArgs e)
        {
            Response.Redirect("http://web-designing-developing-tutorials.blogspot.in/");
        }

    }
}
Now create a webform (z-test2.aspx) and write the code in code behind (z_test2.aspx.cs).
using System;
namespace AspDotNets
{
    public partial class z_test2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Write("String receive from session variable Before_redirect =" + Session["Before_redirect"] + ""); 
            Response.Write("String receive from session variable After_redirect =" + Session["After_redirect"]);        
        }
     }
}
When click on Button 1

When click button1 than simply transfer to page z-test2.aspx. In button 1 we does not transfer any value to z-test.aspx.cs because we does not use session variable in button1.

But if click first button 2, or 3 than click one than session store string in session variable because Session variable store by default 20 min. In detail we study later part.

When click on Button 2

String store in session variables “Before_redirect” and “After_redirect” just before and after Response.Redirect, these session variables strings will be accessed on another page (we name it z-test2.aspx).

If we try to access these session on the other page both before and after session will be accessed because execution of current page is not terminated when EndResponse values is false. i.e code after Response.Redirect will be accessed.


When click on Button 3

Similarly as in button2 string store in session variables “Before_redirect” and “After_redirect” just before and after Response.Redirect, these session variables strings will be accessed on another page.

If we try to access these session on the other page only before Response.Redirect session will be accessed and after session will not be accessed because execution of current page is terminated when EndResponse values is true.


When click on Button 4

When click button4 it move to web-designing-developing-tutorials.blogspot blogger.

Note:-

1. The button 3 rule also apply for button 1 because EndResponse by default true if not use EndResponse.

2. If you use our code to learn than first click on Button1, Button2, than Button3 on order because Session variable store by default 20 min. In detail we study later part.

Understand Response.Redirect flow between client-and-server

When we use response.redirect, there are two request-response cycle round trip will be happend, Suppose, when we click on button immediately postback response is happened for the page and send to web server have code for respose.redirect than web server send response header back to the client and then client initiate new request for the new page and web server receive the request and process and send html page back to the client browser, this type of page navigation technique does not involve two response cycle.

Initial request with redirect                                               
                             ----------------------->

                            Got Response header from server
                           <--------------------------
       Client                                                      Web Server
                              New Request Initiated
                            ------------------------>

                           Response sent back as HTML
                           <-------------------------

NOTE:-

1. Reponse.Redirect is maintained browser history which help to back previous using back button. Redirect can be used to navigate pages/website on the same web server or different web server.

2. Response.Redirect is similar to the HyperLink technique use to navigate any website/Pages on same webserver or different web server.
In the above example, in button4_click event is satisfy this rule. 3. Due to Round trip it is bit slow.

4. After sending headers to the client and try to redirect, you receive an HttpException exception error.

Interview Question:-


Q1. What is difference between Response.Redirect true and Response.Redirect false and Rsponse.Redirect?



previous Related Post



0 comments:

Post a Comment