24.3 Advanced Server Behaviors
In addition to the server behaviors described already, you'll find two other sets of
behaviors that come in handy on dynamic Web pages.
|
24.3.1 Recordset Paging
This set of five different behaviors lets you add links for jumping to different records
in a recordset (straight to the last record, for example). In fact, Dreamweaver makes
use of these same behaviors as part of its Recordset Navigation Bar object. You'll use
these for moving through a long list of records, like a complete listing of products
in a database.To begin, add a recordset to a page. It should contain multiple records, since jumping
to the next record when there's only one doesn't make much sense. The page could
also contain a repeating region, so that several records appear.You can add the recordset paging server behaviors from the Server Behaviors panel,
or from the new recordset paging menu on the Application tab of the Insert Bar (see
Figure 24-7).Move to First Record adds a link that jumps to the first record in the recordset.
NOTEIn ColdFusion, ASP.NET, and PHP, you'll see the word Page instead of Record in this example and the
following ones. For example, Move to First Page instead of Move to First Record.Move to Previous Record adds a link that jumps to the record before the current
record. If you use a repeating region, it jumps to the previous set of records. For
example, say you create a repeating region that displays five records at a time. If
the page currently displays records 6-10, clicking a link with this server behavior
applied to it causes records 1-5 to appear.Move to Next Record adds a link that jumps to the next record in the recordset.
If you use a repeating region, it jumps to the next set of records.Move to Last Record adds a link that goes to the last record in the recordset.Move to Specific Record adds a link that goes to a single record based on information
passed in the URL.
You can create your own recordset navigation controls using
the recordset paging server behaviors.

Using any of the first four behaviors involves the same steps:Create a recordset and add dynamic content to the page.
For example, you could create a list of all products your company sells. The recordset
should contain at least enough records to span several pages. (You wouldn't use
any of these behaviors if you display all of the records on a single page.)Click the page where you wish to insert the link.
You can also select an item on the pagetext or a graphicthat you'd like to
turn into a link.From the Server Behaviors panel, click the + button. Select Recordset Paging, and
then choose a behavior from the submenu.
The window for the particular server behavior appears (see Figure 24-8).Choose a recordset from the Recordset menu.
This is the recordset the behavior will move through.The recordset paging behaviors can add
a new link, or add a link to text or an
image you've selected on the page. You
can also use the menu to select any link
already on the page. That's usually not a
good idea, however, since Dreamweaver
will erase whatever link was previously
applied.

Click OK.
Dreamweaver adds the server behavior to the page and adds its name to the Server
Behaviors panel.
The Move to Specific Record behavior works a bit differently. It doesn't add a link
to the page; it simply forces the page to display a particular record when the page is
requested. It does this by looking for a URL parameter that specifies a particular record
in the recordset. In general, this isn't a good approach, since it requires the database
server to retrieve many records just to display a single item from the database, which
is a waste of server resources. On a busy site, that could mean a slowdown. You're
better off creating a detail page (Section 22.7), which simply retrieves one record from
the database.
24.3.2 Show Region Server Behaviors
At times, you'll want to display different information on a page based on the results
of a recordset. For example, if a visitor searches your site's database of products for
a product you don't sell, the search results page should say something like, "Sorry,
we don't carry alligator skin bicycle seats." But if someone searches for a product
you do sell, then the page should present the relevant details for that product. The
Web page displays different text depending upon whether the search item was in the
recordset.Dreamweaver provides three sets of server behaviors that let you display any selection
of HTML based on the results of a recordset.Show If Recordset Is Empty. If the recordset retrieves no records, this behavior
will make the selected HTML appear in the browser window.This behavior comes in handy for a search page. On the search results page, apply
it to some text like "We're sorry your search retrieved no results," and you've got
yourself a friendly solution for searches that turn up empty.Show If Recordset Is Not Empty. If the recordset retrieves any records, this behavior
will cause the selected HTML to appear in the browser window: a list of search
results or details on a specific database record, for example.Show If First Page. This server behavior, like the next three, works in conjunction
with recordset paging behaviors. It makes the selected HTML appear when the
page displays the first record of a recordset, which comes in handy when you want
to let people step through several pages of records.Show If Not First Page is the opposite of the previous one. If the page does not
contain the first record in a recordset, then the selected HTML appears.Dreamweaver makes use of this behavior in its Recordset Navigation Bar (see Section 22.4). In that case, if the page displays anything except the first item in a recordset,
the First Item and Previous Page links appear. If the page does display the first
item in a recordset, those links are hidden. (After all, you can't very well view the
Previous Page if you're on Page 1.)Show If Last Page functions just like the Show If First Page behavior, but for the
last record in a recordset.Show If Not Last Page functions just like the Show If Not First Page behavior, but
for the last record in a recordset. Dreamweaver uses this behavior to hide or show
the Next Page and Last Item links in the Recordset Navigation Bar (Section 22.4).
The Show Region server behaviors are available on
both the Application tab of the Insert bar and the
Server Behaviors panel.

You can use these behaviors to show any selected object on the pagea paragraph of
text, an image, a table, and so on. Your page can contain any combinations of these
behaviors, and you can use any behavior two or more times to display multiple areas
of a page. For example, maybe you'd like two things to appear when the recordset
successfully retrieves a record: a graphic in the page's sidebar and a message in the
main area of the page. You'd apply a Show If Recordset Is Not Empty server behavior
to both selections of HTML (the graphic and the message).You'll often use these behaviors in pairs. For example, a search results page should
include both the "Show If Recordset Empty" behavior to display a "no results" message,
and a "Show If Recordset Not Empty" behavior to display the search results.To apply any of these behaviors:Create a dynamic page containing a recordset.
This could be search results page, or a master page that lists many records from
the database.Select the HTML you wish to show based on a recordset outcome.
For example, when applying a Show If Recordset Is Empty server behavior to a
search result page, select the message that should appear if the search returns no
results.Open the Server Behaviors panel (Window
button, select Show Region, and choose one of the six behaviors from the submenu.
A window like Figure 24-10 appears. While the title of the window varies depending
on which behavior you selected, each of the six behaviors has just this one
option.
NOTEThe last four behaviorsShow If First Page, Show If Not First Page, Show If Last Page, and Show If Not
Last Pagework only on pages that also have one of the recordset paging server behaviors applied.Select the name of the recordset from the menu and click OK.
The recordset you select should be the one whose results you're interested in.
For example, on a search results page, you'd select the recordset you created to
perform the search.
Regardless of which of the Show Region
server behaviors you apply, there's only one
option to choose: the recordset whose results
control the display of the region.

After applying one of these behaviors to a selection of HTML, a gray line appears
around the selection, and a gray tab appears bearing the words "Show If." That's the
area that will appear if the given recordset condition is met (for example, if the page
is displaying the last record of the recordset).To remove a Show Region server behavior, select its name in the Server Behaviors
panel and click the - button (or press the Delete key). Doing so removes the gray tab
and outline. Now the affected HTML will appear regardless of the recordset results.NOTEPHP noteAn extension called PHP Show If Recordset Field Condition Is True (available for free on the
Macromedia exchange) lets you display part of a page when a field from a recordset matches a certain condition.
Suppose, for example, that you have a products database with a field that records whether a particular
item is for sale. If the item is indeed for sale, then you can use this behavior to display a large "For Sale"
graphic on the product's detail page.