Adding Dynamic Text

So far, all the examples we've shown you have dealt with static data that the page author will update when creating the new page. A template can also include dynamic text. By dynamic, we mean that the template will display text from the site's database, and the page author doesn't have to do a thing. To illustrate this capability, we'll add dynamic text to the template that displays the last new book added to the sample Books database.

First, you must connect the template to the book site by defining the site. You learned how to do that in Chapters 2 and 10, so we won't repeat those instructions here. We're also working with a system data source name (DSN) named BooksConnection. (You can learn how to create a DSN in Chapter 4.) We're still working in the local system we mentioned earlier in this chapter:


It doesn't matter where your copy of the sample books database is, but we'll be working with a copy that's in the Chapter23 folder for this example.

Now, let's update the template by adding a dynamic text control that displays the last book entered into the database. That way, the page will always show the most recently donated (and available) book. Follow these steps:

  1. Open the Application window and click the Server Behaviors tab.

  2. Click the plus sign (+) and choose Recordset (Query) from the submenu.

  3. Name the recordset ShowMostRecentBook, and add the following SQL to the SQL control, as shown in Figure 23.34:

    SELECT Last(tblBooks.Title) AS LastOfTitle  FROM tblBooks

    The Connection option should default to your books connection (ours is the DSN Books- Connection). If you like, click Test to see the results of the SQL statement, and then click OK to return to the Recordset dialog box.

    click to expand
    Figure 23.34: Create a recordset that will retrieve the dynamic text.

  4. Click OK, and Dreamweaver MX will warn you about code lying outside the <html> section. You'll need to add the <TemplateInfo> template tag to the <head> section in order for Dreamweaver MX to update pages based on the template. Without this statement, pages won't display the dynamic text. Click OK to clear the message.

  5. Display the template's code, if necessary, and locate the <head> section and add the following tag:

    <!--TemplateInfo CodeOutsideHTMLISLOCKED="true"-->

    as shown in Figure 23.35.Make sure it's in the <head> section-we placed ours just before the closing </head> tag.

    click to expand
    Figure 23.35: Add the <Template Info> tag to the <head> section.

  6. Return to Design view and add a new line after the right-aligned text, "A Harey Tale."

  7. Click the Application tab in the Insert bar.

  8. Click the Dynamic Text tool to display the Dynamic Text dialog box, which is shown in Figure 23.36.

    click to expand
    Figure 23.36: Select the field that contains the data you want to display.

  9. Select LastOfTitle, and click OK.

  10. Left-align the text by choosing Text ® Align ® Left.

  11. Save the change by choosing File ® Save.

  12. When Dreamweaver MX prompts you to update existing template files, select October.asp, click Update, and then click Close. You may see another warning about code outside the <html> tag. Click OK to clear it, and then close the base template.

  13. In your browser, enter the address to October.asp. Ours is http://localhost/ masteringdreamweaver/chapter23/october.asp. If the page is current, click Refresh.You may need to hold down the Shift key while clicking Refresh. The page in Figure 23.37 shows the dynamic text. The displayed book title will change when you add a new book to the database.

    click to expand
    Figure 23.37: Our template-based page displays data from our Books database.

Mastering Dreamweaver MX Databases
Mastering Dreamweaver MX Databases
ISBN: 078214148X
EAN: 2147483647
Year: 2002
Pages: 214 © 2008-2017.
If you may any questions please contact us: