year ago, if someone had asked me to develop an interactive Web site, I’d have said, “Sure, me and what magic genie?” Fortunately, for those of us with little expertise in Web development, Dreamweaver MX is that magic genie. In this article, you’ll see how to generate Dreamweaver MX server behaviors so you can display and interact with dynamic data?without really knowing anything about how it works.
What Is a Server Behavior?
In Dreamweaver MX, the term behavior refers to software-generated script that executes a specific task. The term server behavior simply means the server executes the behavior script (instead of the client). Dreamweaver MX automatically generates the script necessary to perform these tasks, with just a little help from you. Behaviors automate several data-centric tasks, including:
- Displaying data, and letting users modify and navigate through the data.
- Searching the data.
- Restricting access to data.
The Server Behaviors sidebar contains a list of server behaviors and a brief explanation about how you might use each type.
To get started, define a new site and create a connection to the data source. If you don’t know how to build a connection, add a document, or create a recordset, search Dreamweaver MX’s Help feature for these topics:
- Connecting to a database
- Create and save a new page
- Creating a recordset without writing SQL.
Setting up the Examples
Before you can insert a server behavior into a page, you must have a working connection to a database and a recordset object. The examples in this article depend on Microsoft technologies (Microsoft Access, ASP, IIS, and VBScript), but you can use other databases and other server-and-client technologies and languages.
- Internet Information Services (IIS) for the Web server.
- Active Server Pages (ASP) and VBScript to retrieve data.
- MS Access to store data. (We copied Northwind.mdb to a sample file directory? C:InetpubwwwrootDreamweaverSampleFiles.)
- A Data Source Name (DSN) ODBC connection named NWJetfortheWeb.
How to Add a Server Behavior
|Figure 1: Choose a behavior to add it to the current page.|
After opening an ASP page and creating a recordset, you’re ready to start adding behaviors. To do so, click the plus (+) symbol in the Server Behaviors tab in the Application panel. Then, choose a behavior from the resulting submenu (see Figure 1).
After you choose a behavior, Dreamweaver MX inserts one or more code blocks, known as participants, into your page, and adds the new behavior, by name, to the Server Behaviors tab. You can select the name in the Server Behaviors tab to quickly locate the behavior. We selected a Repeat Region behavior in the current page (see Figure 2) that displays employees (FirstName and LastName fields) from the Northwind Employees table.
Perhaps the biggest obstacle you’ll face is deciding exactly where to have Dreamweaver MX insert the new block of code for the behavior. Unfortunately, there’s no general guideline you can follow. The key lies in knowing what the script does and what text, statement, or object that script acts upon. Usually, you’ll select the corresponding text, statement, or object before inserting the script. On the bright side, positioning doesn’t always matter?some behaviors execute successfully regardless of where you put them, as long as they’re within the body of the page. The list of server behaviors in the Server Behaviors sidebar includes general comments about positioning.
|Figure 2: Select a behavior to highlight its corresponding code.|
Be careful when modifying your page. If you manually alter a participant (a single ASP tag) in any way, even just to delete or add a space or line break, Dreamweaver MX will no longer display the behavior in the Server Behaviors tab. The participant will still exist in the page and it will still work. However, you won’t be able to access it quickly anymore?you’ll have to find it yourself by viewing the script.
Using Server Behaviors?a Simple Example
|Figure 3: By default, a page displays only one record.|
This example uses the Northwind database (you can use any database as long as you’re familiar with the structure) that you copied to the DreamweaverSampleFiles folder. Create a recordset object and populate it with the FirstName and LastName fields from the Employees table.
To add dynamic fields to a page, drag the fields to the Design View window from the Bindings tab in the Application panel. Or, click the Server Behaviors tab, click the + symbol and choose Dynamic Text. Then, select the appropriate field from the list and click OK. Be sure to insert a space character between the two fields. Name the page employees.asp.
By default, the page will display only a single row of data. For example, the script shown in Figure 3 displays only one record?in this case, it’s the record for Nancy Davolio.
|Tip: Click the Live Data View tool to display data in the Design View window.|
To display multiple records, you must repeat the behavior that displays the single record using what’s known as the Repeat Region behavior. The Repeat Region behavior adds a looping structure to the code that retrieves the records. Here’s the procedure to add a Repeat Region behavior to the script shown in Figure 3:
- Click the Server Behaviors tab.
- Select the dynamic field statements?the text fields you added during the above drop and drag task. To do so, simply highlight both fields in the Design View window.
- Click the + symbol and then choose Repeat Region from the resulting submenu. Enter 5 in the The Records at a Time control, and then click OK in response to the Repeat Region dialog box (see Figure 4). The Records at a Time option determines how many records the page displays at one time. Dreamweaver MX adds the appropriate participant to the page. The behavior also reset the Repeat1_numRows = 0 value to display five records per page. Scroll up to see the change in the Code View window.
- Position the insertion point after the LastName field in the Design View window and press Return to add a line break between each employee record.
- Click the Live Data View tool so you can see the results in the Design window (see Figure 5).
|Figure 4: Enter 5 in the Repeat Region dialog box.|
After adding the Repeat Region behavior, the page displays five records rather than one. However, that’s not all the records available in the database. Without further modifications, the user has no way of knowing that there are more records. To display any remaining records, you must add a Recordset Paging behavior. This behavior lets users move forward and backward through the data?in this case, five records at a time. To add the Paging behavior:
- Position the insertion point below the Repeat Region fields in the Design View window.
- In the Server Behaviors tab, click the + symbol.
- Choose Recordset Paging, and then select Move To Next Record from the resulting submenu. Doing so adds the tag (see Figure 6). Click the Live Data View tool to display a link below the displayed records in Design View. You’ll have to open the page in a browser to see the link actually work.
Open the sample employees.asp page in your browser. As expected, the first page displays the first five employee names, and clicking the Next link displays the remaining records. There are only nine records so the link actually displays the last four records. If you click the Next link again, nothing changes. That’s because there are no more records to display, but your user may not know that. You can add the Show Region behavior to share information of this sort with the user. For example, to add a Show Region behavior that tells the user there are no more records in the recordset to our sample page, follow this procedure:
- Add the text you want displayed when there are no more records?we typed in “There are no more records to display” in the Design View window below the Next link.
- Select the text.
- Click the + symbol in the Server Behaviors tab.
- Choose Show Region from the menu.
- Select Show Region If Last Record from the resulting submenu.
- The Show Region If Last Record should display the rstEmployees recordset object, so click OK.
- Dreamweaver MX encloses the text in an If statement (see Figure 7) that determines whether or not the text is displayed. To see the actual results, open employees.asp in a browser and click the Next button (see Figure 8).
|Figure 7: This behavior adds an If statement to the page.|
|Figure 8: Now the user knows there are no more records.|
More Options, Less Work
The end result of adding these behaviors is a script that’s over 300 lines long, because Dreamweaver MX inserts all the paging options by default (even if they’re not required). The generated script might not be the most efficient code possible, and perhaps you’d make different choices if you wrote the script yourself. However, you didn’t have to write it, and that’s the whole point. You needed no scripting language expertise and little time to create this page; simple point-and-click operations sufficed to have Dreamweaver MX generate the script. DreamWeaver MX’s script-generation capabilities highlight an increasingly important development trend, where developers make smart choices, but smart development environments do the grunt work of writing the code. You no longer need to be an expert at several scripting languages to build data-driven Web pages?you just need Dreamweaver MX.