Popular Posts

Friday 15 July 2011

Response.Write VS Server.Transfer ASP.Net

Response.Write 

This Method is used to write a string to HTTP output. It is very useful and mostly used to write out the content of variables.

Write this in .cs class on Page_Load Event:

protected void Button1_Click(object sender, EventArgs e)
{
Response.Write("Welcome ASP.Net !!!"  );
}

Server.Transfer

The Transfer method sends (transfers) all the state information (all application/session variables and all items in the request collections) created in one ASP file to a second ASP file.




When the second ASP page completes its tasks, it will NOT return to the first ASP page (like the Execute method).
Note: The Transfer method is an efficient alternate for the Response.Redirect. A redirect forces the Web server to handle an extra request while the Server.Transfer method transfers execution to a different ASP page on the server, and avoids the extra round trip.
Syntax:

Server.Transfer(path)

Example:

Page Default1.aspx
protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Write("Page1 Content. Part 1 <br />");
        Response.Write("Page1 Content. Part 2 <br />");
        Server.Transfer("Default2.aspx" );       
    }

Page Default2.aspx

protected void Page_Load(object sender, EventArgs e)
    {
Response.Write("Page 2 Content <br />");
     }

Run this project and Click Button1, It will redirect on Other Page named Default2.aspx


Result Will Look like this

Page1 Content. Part 1
Page1 Content. Part 2
Page 2 Content

No comments:

Post a Comment