Difference between Server.Transfer and Response.Redirect in Asp.Net

This asp.net tutorial explains, what is the difference between Server.Transfer() and Response.Redirect() in Asp.Net.

Difference between server.transfer and response.redirect in asp.net

Below are the difference between server.transfer and response.redirect in asp.net.

Response.Redirect() in Asp.Net

  • HttpResponse.Redirect() method is used to send the user to another page.
  • Example: Response.Redirect(“~/Page2.aspx”);
  • When you use the Redirect() method, ASP.NET immediately stops processing the page and sends a redirect message back to the browser. Any code that occurs after the Redirect() call won’t be executed.
  • When the browser receives the redirect message, it sends a request for the new page.
  • Redirect() method can send the user to any type of page.
  • Even you can send to other web sites like below:
  • Response.Redirect(“https://www.enjoysharepoint.com”);

Server.Transfer() in Asp.Net

  • HttpServerUtility.Transfer() method also used to send the user from one page to the other page.
  • Ex: Server.Transfer(“Page2.aspx”);
  • It doesn’t involve the browser. Instead of sending a redirect message back to the browser, ASP.NET simply starts processing the new page as though the user had originally requested that page.
  • You can’t use Transfer() to send the user to another website or to a non-ASP.NET page like HTML page.
  • The Transfer() method allows you to jump only from one ASP.NET page to another, in the same web application.
  • When you use Transfer(), the user won’t have any idea that another page has taken over, because the browser will still show the original URL. Means the URL will not change.

Asp.Net response.redirect example

Now, we will discuss how to redirect to the google search page in asp.net and how to use response.redirect in asp.net in new tab.

Redirect to google search page in asp.net

Now, we will see, how to redirect the user to the google search page in asp.net. Normally there will be a search box and a button on it. Whenever the user clicks on the button, the user should be redirected to the google page with the search parameter.

Find the below code:

.Aspx Code:

<asp:TextBox ID="txtSearch" runat="server" Height="30px" Width="280px" ></asp:TextBox>
<asp:Button ID="btnSearch" OnClick="btnSearch_Click" Text="Search" runat="server" />

Code Behind Code:

protected void btnSearch_Click(object sender, EventArgs e)
{
string search = txtSearch.Text.Trim()+" site:EnjoySharePoint.com";
Response.Redirect("https://www.google.com/search?q=" + search);
}

This will open the Google search results page on the same page on your web site.

response.redirect in asp.net in new tab

Now, we will see how to redirect to a new tab in asp.net. By default when you use Response.Redirect(“Page URL”) will open in the same tab. But my requirement was to open in a new tab instead of the same page.

By default the button code will be like this:

<asp:Button ID="btnSearch" OnClick="btnSearch_Click" Text="Click Here" runat="server" />

And the button click code will be simple:

Response.Redirect("https://www.google.com/");

To open in new tab add the below tag in the button control in .aspx page

OnClientClick="document.forms[0].target = ‘_blank’;"

The button code should look like below:

<asp:Button ID="btnSearch" OnClick="btnSearch_Click" Text="Click Here"
runat="server" OnClientClick="document.forms[0].target = ‘_blank’;" /></div>

You may like following asp.net tutorials:

Here, we learned the difference between server.transfer and response.redirect method in asp.net.

Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

Get update on Webinars, video tutorials, training courses etc.

>