Explanation:
To Transfer or Redirect your server to some specail page there are two methods to use.
- Responce. Redirect
- Server.Transfer
These both methods can be used according to requierd senario. Let us now see this in the detail.
Response.Redirect simply sends a message down to the browser, telling it to move to another page. So, you may run code like:
Response.Redirect("OtherPage.aspx")
or
Response.Redirect(http://www.abc.com/) to send the user to another web page.
To create a url we can also Uri Class. We can see an example;
Uri url = new Uri("~/Forum/ForumMain.aspx", UriKind.Relative);
Response.Redirect(url.ToString(), true);
Notice that requierd url have to be the virtual path for the page. To transfer user to other page better area to code is in Page_Load at Postback.
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack){
Uri url = new Uri("~/Forum/ForumMain.aspx", UriKind.Relative);
Response.Redirect(url.ToString(), true);
}
}
Server.Transfer is similar in that it sends the user to another page with a statement such as Server.Transfer("WebForm2.aspx").
However, the statement has a number of distinct advantages and disadvantages.
First of all, transferring to another page using Server.Transfer conserves server resources. Instead of telling the browser to redirect, it simply changes the "focus" on the Web server and transfers the request. This means you don't get quite as many HTTP requests coming through, which therefore eases the pressure on your Web server and makes your applications run faster.
But watch out: because the "transfer" process can work on only those sites running on the server, you can't use Server.Transfer to send the user to an external site. Only Response.Redirect can do that.
Secondly, Server.Transfer maintains the original URL in the browser. This can really help streamline data entry techniques, although it may make for confusion when debugging.
That's not all: The Server.Transfer method also has a second parameter—"preserveForm". If you set this to True, using a statement such as Server.Transfer("WebForm2.aspx", True), the existing query string and any form variables will still be available to the page you are transferring to.
For example, if your WebForm1.aspx has a TextBox control called TextBox1 and you transferred to WebForm2.aspx with the preserveForm parameter set to True, you'd be able to retrieve the value of the original page TextBox control by referencing Request.Form("TextBox1").
Server.Execute actually executes the specified page and then returns back to the original page. This can be used in scenarios where you want to go to a specific page, execute some thing and then come back to the original page.
Top Tip: Don't confuse Server.Transfer with Server.Execute, which executes the page and returns the results. It was useful in the past, but, with ASP.NET, it's been replaced with fresher methods of development. Ignore it.
|