Response.Redirect
- Page URL change in the browser
- Redirect a request to new URL and
Specifies the new URL
- It's used to navigate to
sites/pages on the same web server or outside of the web server.
- Response.Redirect has overloaded
parameter i.e. Boolean by default it's true.
- 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.
- If you need to execution of
current page operation during redirection, set overloaded parameter false,
your code look like-
- 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
- First page URL not change in the
browser
- Redirect a request to new URL by
using the specified URL path of the page.
- It's used to navigate to
sites/pages on the same web server.
- Server.Transfer has overloaded
parameter i.e. Boolean by default it's true.
- Overloaded parameter used to
preserve form data during redirection.
- Server.Transfer terminates the
execution of the current page and start the execution of the new page
- If you don't want to preserve form
data during redirection, set overloaded parameter false, your code look
like-
- 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
- First page URL not change in the
browser
- Redirect a request to new URL by
using the specified URL path of the page.
- It's used to navigate to
sites/pages on the same web server.
- Server.Execute has few overloaded parameter.
- One of overloaded parameter used
to preserve form data during redirection.
- 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.
- If you don't want to preserve form
data during redirection, set overloaded parameter false, your code look
like-
- 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