Deleting a Record

We're going to follow a slightly different method for deleting a record than we did for updating a record. This time, we're going to use the Delete Record Server Behavior. Follow these steps:

  1. Add [Delete] next to the Update link in the Table column.

  2. Highlight the [Delete] link, and add the Go To Detail Page Server Behavior to the link, just as we did before, only this time specifying DeleteCategory.asp as the Detail Page.

  3. Create a new blank page, and add a header such as Delete a Category.

  4. Add a recordset similar to the one we added earlier, except name this one something like DelCat.

  5. Make sure you filter the recordset on the URL parameter CategoryID, as we've done in Figure 20.40.

    Figure 20.40: Our recordset for our delete page is similar to the update page recordset.

Next, we're going to manually create a data display and form on our delete page.

  1. Insert a table that has two columns and two rows.

  2. In the top row, add the table headers Category ID and Category.

Next, we're going to manually add the fields from the recordset to our page. It's easy to do, since we just have to click and drag the fields.

  1. Expand the recordset DelCat on the Bindings tab of the Application panel.

  2. Drag each of the fields CategoryID and Category to the appropriate second-row spot in the table, as we did in Figure 20.41.

    click to expand
    Figure 20.41: We're manually creating a display for the delete page.

  3. To add a Submit button to the page, click the Button icon on the Forms Insert bar. Make sure you choose Submit as the button type.

You might want to name the button Delete Record or some other more meaningful name since this is an irreversible action. If you don't already have a form on the page (which we don't at this time), Dreamweaver MX adds a form around the button for you. (Dreamweaver MX adds the form if your cursor is in the Design window when you add the button. If your cursor is in the Code window, Dreamweaver MX will not automatically add the form.)

Normally, we'd name our form, but since we're only using the button here, we're going to leave the default name of form1.

  1. Click the Submit button to select it.

  2. Add the Delete Record Server Behavior following the technique described earlier.

  3. Again, as you can see in Figure 20.42, we're using the same definitions to define the data connection. Since our recordset name has changed to DelCat, we select it in the Select Record From drop-down list box.

    Figure 20.42: Your Delete Record definition should resemble this one.

  4. Click OK to add this behavior to the button. Delete Record will delete the current record once the user submits the form.

As all good delete actions should behave, ours should ask the user if they really want to pursue this process. We'll accomplish that by adding an OnClick event to the Behaviors tab in the Design panel. Since Submit is a unique button to HTML with a specific purpose, we'll find access to its events by right-clicking the button itself and choosing Edit Tag to open the Tag Editor dialog box. Expand the Events category, click OnClick, and add the following

javascript: return confirm('Are you sure you wish to delete this record?')

into the Input window as we did in Figure 20.43.

click to expand
Figure 20.43: The Submit button's events are hidden in the Tag Editor dialog box.

This code is, of course, a piece of JavaScript that will pop up a confirm message box with the "are you sure" message when the user submits the form. If the user clicks OK in the confirm message box, the submit will continue, and the code will delete the record. If the user clicks Cancel, the submit is canceled, and the record is preserved. Make sure you include the keyword return in the JavaScript; otherwise, the buttons will not function as you intend.

We should also give the user a way to cancel, or back out of, this delete process from the main page. We'll do that by adding another button with an OnClick method that simply jumps back to the BookCategory.asp page. Follow these steps:

  1. Add another button, name it Cancel, and then select the button.

  2. In the Behaviors tab, choose the Go To URL behavior to open the Go To URL dialog box, as shown in Figure 20.44.

    click to expand
    Figure 20.44: Dreamweaver MX provides a behavior that lets us jump to a URL.

  3. Add the BookCategory.asp page in the URL box, and click OK. You'll now see the onClick Go To URL behavior listed in the Behaviors tab when the button is selected.

  4. Save your page as DeleteCategory.asp.

  5. Load BookCategory.asp in your browser, and then click the Delete button. You should see a confirm window like that in Figure 20.45.

    click to expand
    Figure 20.45: The confirmation message after clicking the Delete button

  6. Click OK to delete the record. If you click Cancel, the record is preserved, and the BookCategory page is displayed again.

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: