AJAX In Action [160]
A double-combo script
329
Server
Handles request
Request
Page
page
rendering
JavaScript
Add options
Figure 9.1
Browser
The client-side solution
On the other hand, the JavaScript method has one benefit: after the initial load time, it is fast. There is no major lag between selecting an option from the first selection list and the second list being populated. So this method is only usable if you have just a few double-combination options that will not impact the pageloading time significantly. 9.1.2 Limitations of a server-side solution
The next traditional solution is the submission of a form back to the server, which is known as a page postback. In this method, the onchange event handler in the first selection list triggers a postback to the server, via the submit() method of the form’s JavaScript representation. This submits the form to the server, transmitting the user’s choice from the first select element. The server, in turn, queries a database based on the value that the user selected, and dynamically fills in the new values for the second list, as it re-renders the page. You can see the process of the server-side method in figure 9.2.
A drawback to the server-side method is the number of round-trips to the server; each time the page is reloaded, there is a time delay, since the entire page Server
Build select and
Handles request
rebuild document
Request
Page
Page
page
rendering
rendering
JavaScript
postback
Figure 9.2
Browser
The server-side postback method
Licensed to jonathan zheng 330 CHAPTER 9 Dynamic double combo has to re-render. Figure 9.2 shows all of the extra processing required. Additional server-side code is also needed to reselect the user’s choice on the first select element of the re-rendered page. Moreover, if the page was scrolled to a particular spot before the form was submitted, the user will have to scroll back to that location after the page reloads. 9.1.3 Ajax-based solution We can avoid the problems of the JavaScript and server-side solutions by using Ajax to transfer data to the server and obtain the desired information for the second selection list. This allows the database to be queried and the form element to be filled in dynamically with only a slight pause. Compared with the JavaScript method, we are saving the extra page-loading time that was required to load all of the available options into the arrays. Compared with the server-side postback solution, we are eliminating the need to post the entire page back to the server; instead, we are passing only the information necessary. The page is not reloaded, so you do not have to worry about the scroll position of the page or what option was selected in the first drop-down field. The initial page loading time is also shortened since the JavaScript arrays do not have to be included in the page. This example will involve two selection lists. The first selection list contains the sales regions for a company. The second selection list displays the related territories for the selected region, as shown in figure 9.3. When the user selects a region from the first selection list, the client sends a request to the server containing only the necessary information to identify both the selected region, and the form control to populate with the list of territories. The server queries the database and returns an XML document containing the names of the territories in the selected region, and also the names of the form and the control that the client needs to update. Let’s see how this works. The first step in building the Ajax solution takes place on the client. Server Handles request Options built Request Page Options page rendering Ajax returned Send request Figure 9.3 Browser The Ajax solution Licensed to