Total Pageviews

21 March 2016

Difference between response.redirect() and server.transfer() method

Response.Redirect and Server.Transfer  both are use to transfer user from one page another page both purpose is same.

there are following difference given as below.


 Response.Redirect() -Redirect a URL a new page with proper path
Response.Redirect("secondpage.aspx");
Server.Transfer()-Terminate the current request in a current page and new page using.

Server.Transfer("secondpage.aspx");

On your browser you can click back and update your address bar and send new URL tab. HTML pages on our server or to some other web servers and provide all response of all request.
Response Redirect simply sends a message down to the (HTTP 302) browser. It does not preserve a query sting in page.

Server Transfer  you can not click hit back some times loading page.
transfers current page request to another .aspx page on the same server and preserve query string and form variables. and does not show real URL n address bar.
Server.Transfer happens without the browser knowing anything, the browser request a page, but the server returns the content of another





EXAMPLE-

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="firstpage.aspx.cs" Inherits="firstpage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Button ID="bt1" runat="server" Text="response.redirect" OnClick="bt1_Click" />
 
     <asp:Button ID="Button1" runat="server" Text="server.transfer" OnClick="Button1_Click" />
    </div>
    </form>
</body>

</html>

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class firstpage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void bt1_Click(object sender, EventArgs e)
    {
        Response.Redirect("secondpage.aspx");
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Server.Transfer("secondpage.aspx");

    }
}

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="secondpage.aspx.cs" Inherits="secondpage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    Hello Friends good morning
    </div>
    </form>
</body>
</html>

No comments:

Post a Comment

Contact Form

Name

Email *

Message *