Round Trip and Postback

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:

  1. The user requests a Web form from the Web server.

  2. The Web server responds with the requested Web form.

  3. The user enters the data and submits the form to the Web server.

  4. The Web server processes the form and sends the result back to the user.

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:

  • Maintain values of controls and variables across page postbacks.

  • Distinguish the initial request of a page from the page postback.

  • Provide smart navigation features similar to those of desktop 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 Property

The 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:

  1. Open Visual Studio .NET and create a new blank solution named 315C04 at c:\inetpub\ wwwroot \ExamCram (you might need to change the directory based on your configuration).

  2. Create a new Visual C# ASP.NET Web application project. Specify the location of the project as http://localhost/ExamCram/315C04/Example4_1 .

  3. Change the pageLayout property of the DOCUMENT element of the Web form to FlowLayout . Add a DropDownList Web server control ( ddlCategories ) to the form. Set its AutoPostBack property to true and the TabIndex property to 1 .

  4. Add a Label control ( lblQuestion ) to the Web form.

  5. Add a TextBox control ( txtTitle ) and set its AutoPostBack to true and TabIndex to 2 . Then, add another TextBox control ( txtMessage ) and set its TabIndex to 3 and TextMode to MultiLine (see Figure 4.1).

    Figure 4.1. The design of a form that allows you to post messages to a Weblog.

    graphics/04fig01.jpg

  6. Add a Button control ( btnPost ) and set its Text to Post A Message . Place a Label control ( lblWeblog ) at the end of the form, as shown in Figure 4.1.

  7. Switch to the Code view of the Web form and add the following code to the Page_Load() event handler:

     private void Page_Load(object sender, System.EventArgs e) {     if (!Page.IsPostBack)     {         // If page is requested for the first time         ddlCategories.Items.Add("Web development");         ddlCategories.Items.Add("Programming Languages");         ddlCategories.Items.Add("Certifications");     }     else     {         // On postback, change the case of the textbox's text         txtTitle.Text = txtTitle.Text.ToUpper();     }     // Set the text of the label control on each page load     lblQuestion.Text = "What do you want to write about "       + ddlCategories.SelectedItem.Text + " today?"; } 
  8. Attach the following event handler to the Click event of the Post button:

     private void btnPost_Click(object sender, System.EventArgs e) {     // Format the data entered by the user and     // append it to the existing contents of lblWeblog     lblWeblog.Text = "<b>" +  ddlCategories.SelectedItem.Text              + " :: " + txtTitle.Text + "</b> ("              + DateTime.Now.ToString() + ")<hr>"              + txtMessage.Text + "<p>"              + lblWeblog.Text + "</p>"; } 
  9. Run the project. Use the Tab key to navigate between various fields and publish a few entries to the Weblog, as shown in Figure 4.2.

    Figure 4.2. The Web form retains state for both postback and nonpostback controls.

    graphics/04fig02.jpg

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 Property

ASP.NET has a feature called smart navigation that can greatly enhance the user experience of a Web page by

  • Persisting element focus between postbacks

  • Persisting scroll position between postbacks

  • Eliminating page flash caused by page postback

  • Preventing each postback from being saved in the browser history

graphics/note_icon.gif

Smart navigation is supported only in Internet Explorer 5.0 or later browsers. Therefore, when you are targeting an application for a generic browser, this feature isn't very helpful.


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> 


MCAD Developing and Implementing Web Applications with Visual C#. NET and Visual Studio. NET (Exam [... ]am 2)
MCAD Developing and Implementing Web Applications with Visual C#. NET and Visual Studio. NET (Exam [... ]am 2)
ISBN: 789729016
EAN: N/A
Year: 2005
Pages: 191

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net