Visual Basic Concepts

Step Nine: Write Code for Generated Events

Now it's time to refresh your memory by taking a look back at the Customers page.

The HTML for the Customers page is generated by the code you entered in the Private Sub ShowIDs_Respond event procedure. Part of the code inserts a hyperlink on every row of the HTML table. Each row is a different customer and the hyperlinks link to the customer detail page for that customer. The code that generates the hyperlinks contains this syntax:

"<a href=""" & URLFor("Detail", CStr(rs1("contactID"))) & """>"

At run time, this code will create a hyperlink that looks something like this:

<a href="Webclass1.ASP?WCI=Detail&WCE=1">

A hyperlink usually moves to a web site — such as <a href="http://www.mycompany-inc-10.com">. In this case, the generated hyperlink will tell the system to go to the Detail webitem (WCI=Detail) and launch the UserEvent event with the EventName parameter set to 1 (WCE=1).

As the hyperlink is generated for each row of the table, the webclass event name (WCE) is set to a new ContactID value. Here is more of the HTML that the code creates and sends to the browser. These are the first two lines of the HTML table:

<table border=1 width=90% cellpadding=3>
<TR>
   <TD>1</td>
   <TD><a href="Webclass1.ASP?WCI=Detail&WCE=1">Nancy Davolio</a></td>
   <TD>Cascade Coffee Roasters</td><TD>(206) 555-9857</td>
</tr>
<TR>
   <TD>2</td>
   <TD><a href="Webclass1.ASP?WCI=Detail&WCE=2">Janet Leverling</a></td>
   <TD>Northwind Traders</td><TD>(206) 555-3412</td>
</tr>

In this HTML, the WCE= syntax indicates that a webitem’s UserEvent event (WCE) will be run when this link is clicked. The value that follows (in this case, a number) is the eventname for the event. For the first row of the table above, the eventname is 1, and for the second row, the eventname is 2. (If you had used a different database key, such as lastname, the eventname for the first row would be "Davolio," and for the second row would be "Leverling.")

In order for these event names to be processed, you must write code in a procedure known as the UserEvent procedure. UserEvent is fired whenever the webclass is run with the WCI= and WCE= values specified.

For our example, the actions the webclass must take in the UserEvent code are the following:

  • It must connect to the database.

  • It must determine which row was selected, then retrieve a recordset that contains only the detail record for that row.

  • It must call the WriteTemplate method for the Detail webitem so that the template can be written to the browser.

To write code to process the user event

  1. In the general declarations section of your code, define a new recordset by adding the following line:

    Private rs2 as New ADODB.Recordset
    
  2. In the Detail_UserEvent() procedure, add the same code you used in earlier procedures to open a database connection:

    If cn1.State = adStateClosed Then
        ConnectMe cn1
    End If
    
  3. In the same procedure, add this code to retrieve the appropriate record from the Contacts table. Remember that the UserEvent procedure accepts the EventName parameter (the WCE value from the hyperlink), which in this case is a database key:

    rs2.Open "select * from contacts where contactID=" & EventName, _
        cn1, adOpenStatic, adLockReadOnly
    

    Note   A hyperlink syntax of<a href="Webclass1.ASP?WCI=Detail&WCE=1">Nancy Davolio</a>would cause the Detail template’s UserEvent procedure to be called with the EventName parameter set to 1 (WCE=1). In that case, the code above will retrieve all records from the contacts table where contactID is 1.

  4. Add the following code to call the Detail template’s WriteTemplate method. If a template webitem has any token-prefixed tags, the template’s ProcessTag event will be fired. The Detail template does have these replacement tags and the ProcessTag event is discussed in Step Ten.

Detail.WriteTemplate