Search This Blog

Saturday 16 January 2016

Response.Redirect Vs Server.Transfer Vs Server.Execute in Asp.Net

 Response.Redirect
  1. Page URL change in the browser
  2. Redirect a request to new URL and Specifies the new URL
  3. It's used to navigate to sites/pages on the same web server or outside of the web server.
  4. Response.Redirect has overloaded parameter i.e. Boolean by default it's true.
  5. If you don't specify the overloaded parameter during redirection, it's terminate the execution of the current page and Redirect a request to the new URL.
  6. If you need to execution of current page operation during redirection, set overloaded parameter false, your code look like-
  7. Response.Redirect("~/WebForm2.aspx"false);
To Test Scenario, use Visual Studio debugger-

Step 1:- WebForm1.aspx

protected void Button1_Click(object sender, EventArgs e)
{
    Response.Redirect("~/WebForm2.aspx");
    Response.Write("Welcome Back");           
}

Task 1:- Click button

You will redirected to WebForm2.aspx, you will see current page Response.Write line not executed means current page execution terminated during redirection.

Task 2:- Change Response.Redirect("~/WebForm2.aspx") to Response.Redirect("http://www.google.com/")  and Click button

You will redirected to google.com

Task 3:- Change Response.Redirect("~/WebForm2.aspx") to Response.Redirect("~/WebForm2.aspx", false) and Click button

You will see, you are redirected to WebForm2.aspx as well as current page Response.Write line executed.

Server.Transfer
  1. First page URL not change in the browser
  2. Redirect a request to new URL by using the specified URL path of the page.
  3. It's used to navigate to sites/pages on the same web server.
  4. Server.Transfer has overloaded parameter i.e. Boolean by default it's true.
  5. Overloaded parameter used to preserve form data during redirection.
  6. Server.Transfer terminates the execution of the current page and start the execution of the new page
  7. If you don't want to preserve form data during redirection, set overloaded parameter false, your code look like-
  8. Server.Transfer("~/WebForm2.aspx"false);
To Test Scenario, use Visual Studio debugger-

Step 1:- WebForm1.aspx

protected void Button1_Click(object sender, EventArgs e)
{
    Server.Transfer("~/WebForm2.aspx");
    Response.Write("Welcome Back");           
}

Task 1:- Click button

You will redirected to WebForm2.aspx, you will see current page Response.Write line not executed means current page execution terminated during redirection.

Task 2:- Change Server.Transfer("~/WebForm2.aspx") to Server.Transfer("http://www.google.com/")  and Click button

You will get runtime exception, because www.google.com not hosted on the same web server.

Task 3:- Change Server.Transfer("~/WebForm2.aspx") to
Server.Transfer("~/WebForm2.aspx", false) and Click button

You will see, you are redirected to WebForm2.aspx and you will receive a runtime error because you set the preserve form data overloaded parameter false.

To test this scenario, do following modification

In WebForm1.aspx (Add TextBox control in design and set TextBox Text on Button Click)

protected void Button1_Click(object sender, EventArgs e)
{
    TextBox1.Text = "WebForm1 TextBox";
    Server.Transfer("~/WebForm2.aspx");
    Response.Write("Welcome Back");           
}

In WebForm2.aspx (on Page load try to retrieve WebForm1 TextBox control value)

System.Collections.Specialized.NameValueCollection obj = Request.Form;
Response.Write(obj["TextBox1"].ToString());


Server.Execute

  1. First page URL not change in the browser
  2. Redirect a request to new URL by using the specified URL path of the page.
  3. It's used to navigate to sites/pages on the same web server.
  4. Server.Execute has few overloaded parameter.
  5. One of overloaded parameter used to preserve form data during redirection.
  6. Sertver.Execute process the second Web form without leaving the first Web Form. After completing the execution of the second web form, the control returns to the first web form.
  7. If you don't want to preserve form data during redirection, set overloaded parameter false, your code look like-
  8. Server.Execute("~/WebForm2.aspx"false);


To Test Scenario, use Visual Studio debugger-

Step 1:- WebForm1.aspx

protected void Button1_Click(object sender, EventArgs e)
{
    Server.Execute("~/WebForm2.aspx");
    Response.Write("Welcome Back");           
}

Task 1:- Click button

You will see, Sertver.Execute process the second Web form without leaving the first Web Form. After completing the execution of the second web form, the control returns to the first web form and executed the next line of code.

Task 2:- Change Server.Execute("~/WebForm2.aspx") to Server.Transfer("http://www.google.com/")  and Click button

You will get runtime exception, because www.google.com not hosted on the same web server.

Task 3:- Change Server.Execute("~/WebForm2.aspx") to
Server.Execute("~/WebForm2.aspx", false) and Click button

You will see, you are redirected to WebForm2.aspx and you will receive a runtime error because you set the preserve form data overloaded parameter false.

To test this scenario, do following modification

In WebForm1.aspx (Add TextBox control in design and set TextBox Text on Button Click)

protected void Button1_Click(object sender, EventArgs e)
{
    TextBox1.Text = "WebForm1 TextBox";
    Server.Execute("~/WebForm2.aspx");
    Response.Write("Welcome Back");           
}

In WebForm2.aspx (on Page load try to retrieve WebForm1 TextBox control value)

System.Collections.Specialized.NameValueCollection obj = Request.Form;
Response.Write(obj["TextBox1"].ToString());

No comments:

Post a Comment