AJAX In Action [163]
document. The result set from the SQL query is used to create an XML document that is returned to the client side. Figure 9.6 shows the flow of the serverside process. The server-side code is invoked by the request sent from the client-side ContentLoader object. The server-side code first retrieves the value of the request parameter q, representing the selected region. The value of q is used to create a Licensed to jonathan zheng Implementing the server: VB .NET 335 Database Posted Dynamic Build XML Return Figure 9.6 form SQL document document Server-side process flow diagram dynamic SQL query statement, which is run against the database to find the text/ value pairs for the second drop-down list. The data that is returned by the database query is then formatted as XML and returned to the client. Before we write the code to do this, we need to define the basic XML document structure. 9.3.1 Defining the XML response format We need to create a simple XML document to return the results of our database query to the client. It will contain the options to populate the second selection list. A pair of elements is needed to represent each option, one to contain the option text, and one to contain the option value. The XML document in our example has a root element named selectChoice, containing a single element named selectElement, followed by one or more entry elements. selectElement contains the names of the HTML form and selection list that the results will populate on the client. Each entry element has two child elements, optionText and optionValue, which hold values representing each territory’s description and ID. Listing 9.3 shows this structure. Listing 9.3 Example of the XML response format Licensed to jonathan zheng 336 CHAPTER 9 Dynamic double combo Notice in the example XML document in listing 9.3 that there is an entry containing the option text “Select A Territory”. This is the first option shown in the selection list, prompting the user to choose a value. The server-side code includes this value at the start of every response document, before the dynamic options are obtained from the database. Now that we have our response document defined, we can develop the code that dynamically creates the XML and returns it to the client. 9.3.2 Writing the server-side code The VB .NET server-side code is straightforward. We perform a query on a database, which returns a record set. We then loop through the record set to create our XML document and send the XML back to the client. If we do not find any records, then we do not create any entry elements, also omitting the static “Select A Territory” option. As you can see in listing 9.4, the server-side code is not very complicated. It simply contains statements to retrieve the form values posted to the server, set the content type, perform a search, and output the XML document. This example uses the Northwind sample database from Microsoft’s SQL Server. Listing 9.4 DoubleComboXML.aspx.vb: Server-side creation of the XML response Private Sub Page_Load( _ Implement ByVal sender As System.Object, _ Page_Load ByVal e As System.EventArgs) _ method Handles MyBase.Load Response.ContentType = "text/xml" b Set the content type Dim strQuery As String strQuery = Request.Form("q") c Retrieve the Dim strForm As String posted data strForm = Request.Form("f") Dim strElem As String strElem = Request.Form("e") Dim strSql As String = "SELECT " & _ "TerritoryDescription, " & _ "TerritoryID" & _ d Create the SQL " FROM Territories"