Thursday 5 November 2015

Server.Execute in Asp.Net


Server.Execute 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.Execute is having five overloading versions. I will discuss two of overloaded version here.
  1. Server.Execute(string path); 
  2. Server.Execute(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.Execute("~/Page2.aspx");
  2. Server.Execute("~/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.Execute("~/Page2.aspx", true);
}

Difference Between Server.Transfer & Server.Execute
Server.Transfer terminates the execution of Page 1 (current page) and starts execution of Page 2 (new page), where as Server.Execute process the Page 2 without leaving the Page 1.

Similarity Between Server.Transfer & Server.Execute

  1. Browser Remains on the first page URL.
  2. Both can only be used to navigates to site page on same web server.
  3. Both preserves Form variables from the original request.

No comments:

Post a Comment