Creating the Master Page

A master-detail page pairing is necessary for the update and deletion functionality, because you have to allow the users to select which country they want to update or delete. In this task, you'll create a simple master-detail page consisting of a table with each country's name, a link to modify the country, and a link to delete the country in a row. As before, each of these links will pass a URL parameter identifying the country that the user has selected.

  1. Open admin_cp_master.asp, and switch to code view.

    As before, you'll create the recordset before laying out the page, and since you'll handwrite the code necessary to create the recordset, you need to be in code view.

  2. Between the Restrict Access server behavior code and the beginning of the document itself (just before the <?xml> tag), enter the code necessary to create a recordset ("rs_countries") that retrieves the country name and ID of every record in tbl_country.

    In ASP:

    [View full width]
    <% Dim dbConn Dim rs_countries set dbConn = Server.CreateObject("adodb.connection") dbConn.open("newland") Set rs_countries = dbConn.Execute("SELECT countryID, countryName FROM tbl_country ORDER graphics/ccc.gifBY countryName ASC") %>

    In ColdFusion:

    <cfquery name="rs_countries" datasource="newland">
    SELECT countryID, countryName FROM tbl_country ORDER BY countryName ASC
    </cfquery>
    

    Aside from the details of the SQL statement, this code is identical to that used earlier in this lesson, so you should understand it. The query itself is also easy to read: It retrieves the country name and unique ID, ordered by country name.

    graphics/16inf08.gif

  3. ASP users only: Add the code to close and destroy the recordset at the end of the document.

    <%
    rs_countries.Close()
    Set rs_countries = Nothing
    %>
    

    Again, this code prevents the recordset from wasting memory on the server.

    graphics/16inf09.gif

  4. Switch to design view, and type the following two lines of text:

    Select a country to modify or delete.

    Caution: Deleting is instant and permanent.

    Again, providing directions for the user is critical to the success of your applications.

    graphics/16fig09.gif

  5. Return to code view, and below the paragraph you just created, add the code to create a new table with one row and three columns.

    <table width="98%" border="1" cellspacing="0" cellpadding="3">
      <tr>
        <td>XX</td>
        <td>XX</td>
        <td>XX</td>
      </tr>
    </table>
    

    This creates the basic framework for the table. When you are done with it, its contents will be dynamically generated, with each record being mapped to a single row. Since you can't know the number of rows in advance, you'll have to loop through the recordset, creating a new row for each record.

  6. Enter the appropriate code needed to create a looping structure that encloses the <tr> tag. The code should wrap outside the opening and closing <tr> tags.

    In ASP:

    <%
      Do Until rs_countries.EOF
    %>
        <tr>
          <td>XX</td>
          <td>XX</td>
          <td>XX</td>
       </tr>
    <%
      rs_countries.MoveNext()
      Loop
    %>
    

    In ColdFusion:

    <cfoutput query="rs_countries">
      <tr>
        <td>XX</td>
        <td>XX</td>
        <td>XX</td>
      </tr>
    </cfoutput>
    

    In this step, you are in effect creating a repeat regionyou just aren't using the Dreamweaver behavior. And in spite of their cosmetic differences, both ASP and ColdFusion use remarkably similar logic. Both have a block of code before and after the section to be looped over (the <tr> section). Both specify a query and create a loop that breaks only when the recordset runs out of records: ASP's EOF accomplishes this explicitly, while the recordset loop is implied in ColdFusion, when you specify the query attribute in a <cfoutput> tag.

    graphics/16inf10.gif

    Next, you need to build the contents of the table cells, which will contain a mix of HTML and ASP or ColdFusion code.

  7. Create the static HTML structure using placeholders for the content inside the <td> tags.

    [View full width]
    <td>Country Name</td> <td><a href="admin_cp_update.asp?countryID=CountryID">Modify this country's ®profile</a></ graphics/ccc.giftd> <td><a href="admin_cp_delete_processor.asp?countryID=CountryID">Delete </a></td>

    This code is the same for both ASP and ColdFusion (except that the file extensions in the href attribute for ColdFusion should be .cfm rather than .asp), because it is merely static HTML. We're using placeholders here to ensure that we get the HTML syntax right, before adding the dynamic code.

    graphics/16inf11.gif

  8. Replace the three placeholders with dynamic output, as follows.

    In ASP:

    [View full width]
    <td><%=rs_countries("countryName")%></td> <td><a href="admin_cp_update.asp?countryID=<%=rs_countries("countryID")%>"> Modify this graphics/ccc.gifcountry's profile</a></td> <td><a href="admin_cp_delete_processor.asp?countryID=<%=rs_countries ("countryID")% graphics/ccc.gif>">Delete</a></td>

    In ColdFusion:

    [View full width]
    <td>#countryName#</td> <td><a href="admin_cp_update.cfm?countryID=#countryID#">Modify this country's profile</ graphics/ccc.gifa></td> <td><a href="admin_cp_delete_processor.cfm?countryID=#countryID#">Delete </a></td>

    You've seen plenty of ASP and ColdFusion output code in the course of this book, so the code should be easy to read, especially if you remember that <%= in ASP is equivalent to <% Response.Write().

    graphics/16inf12.gif

  9. Save, upload, and test the file in a browser.

    The table has as many rows as tbl_country has records. If you hover your mouse over one of the links, you'll see not only the URL, but also the countryID parameter attached with the correct country ID.

    graphics/16fig10.gif