Thursday 5 November 2015

Server.Transfer in asp.net



Server.Transfer also exposes server side event just like as Response.Redirect. However there are some differences as well.

Now Again we have same problem. We wants to move from Page1.aspx to Page2.aspx on Button click.
Server.Transfer is having three overloading versions. I will discuss two of overloaded version here.
  1. Server.Transfer(string path); 
  2. Server.Transfer(string path,bool preserveForm);
path is given as 
preserveForm ensures that the posted form values can be retrieved means if we post a page from Page 1 to Page 2 and we want to have Page 1 data values to be available on Page 2. All we have to do is send value of preserveForm=true. Well by default it is true so it is kind of optional.

Example:
  1. Server.Transfer("~/Page2.aspx");
  2. Server.Transfer("~/Page2.aspx",true);
 So all we have to do is drag and drop a button and then we can generate event handler for it by double clicking on the button.

protected void Button1_Click(object sender, EventArgs e)
{
        Server.Transfer("~/Page2.aspx", true);
}

Difference Between Server.Transfer & Response.Redirect

Server.TransferResponse.Redirect
RedirectionRedirection is done by the server.Redirection is done by the browser client.
Browser URLDoes not change.Changes to the redirected target page.
When to useRedirect between pages of the same server.Redirect between pages on different server and domain.

No comments:

Post a Comment