A Web application has a distributed execution model. When a user interacts with a Web form, the browser might respond to some of the user actions by executing client-side scripts while sending some other actions that require server resources to the Web server for processing. When server-side processing is involved, a typical interactive user session with a Web form consists of the following steps:
Step 3 is referred to as a page postback , whereas steps 3 and 4 are collectively referred to as a round trip . A round trip involves making a complete trip over the network to the Web server and getting the response back. The Web applications use Hypertext Transmission Protocol (HTTP) to establish communication between the Web browser and Web server. HTTP is disconnected in nature, which means the life cycle of a Web page is just a single round trip. Between two page requests, the Web server and the clients are disconnected from each other and the values of page variables and controls are not preserved across the page requests. This model of execution enables a Web server to support a large number of clients because each client request occupies the server resources only for a short duration. However, the disconnected nature of HTTP provides a major challenge to Web developers, who must implement the following functionality in their applications:
ASP.NET provides solutions to these problems built into the programming framework. From the discussion about server controls in Chapter 3, "Controls," you already know that server controls automatically retain their values across page postbacks. You will learn how ASP.NET actually retains the states for server controls later in this chapter in the section titled "State Management." For now, I'll talk about two properties of the Page class IsPostBack and SmartNavigation that provide the other two functionalities from the previous list. The IsPostBack PropertyThe IsPostBack property of the Page class returns true when a page is being loaded in response to a client postback. If the page is being requested for the first time, the value of the IsPostBack property is false . A typical case in which you would use this distinction is when you do not want the server to execute costly initialization operations for each page postback. Instead, the initializations are to be performed only with the first request to the page. The following example helps you understand round trip and postback operations and demonstrates the use of the IsPostBack property:
The previous example uses the event handler for the Load event of the Page class to check whether the page is loaded by a postback operation. If that is the case, executing the code for adding items to the drop-down list is skipped . Also note that the navigation between controls is not smooth. When the form returns after a postback, it does not remember the active control. However, the SmartNavigation property can solve this problem. The SmartNavigation PropertyASP.NET has a feature called smart navigation that can greatly enhance the user experience of a Web page by
Smart navigation is specified by the SmartNavigation attribute of the Page directive. To enhance the Web form created in the previous example to use the smart navigation feature, you need to modify its Page directive to the following: <%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="Example4_1.WebForm1" SmartNavigation="true" %> Smart navigation is very useful for intranet applications where you have more control over the browsers used by the users. For such applications, you might want to turn on smart navigation for the complete Web application instead of individual files. You can easily accomplish this by making the following changes to the web.config file of the Web application: <configuration> <system.web> <pages smartNavigation="true"/> </system.web> </configuration> |