When the webclass processes a request from the browser, it must send the browser a response. Generally, that response is a stream of HTML that the browser displays to the user. You can return HTML to the browser in two ways:
The WriteTemplate method is the simplest way to send HTML to the browser in response to a user action. When the webclass launches an event procedure for a template event that contains this method, it sends the template's HTML back to the browser. The resulting page appears to the user.
Note If the template that you want to send to the browser contains any replacement indicators, the webclass fires the ProcessTag event before it sends the response. This happens automatically as part of WriteTemplate method processing.
The following code shows an example of how you would use this method in code:
Private Sub OrderSearch_Respond()
OrderSearch.WriteTemplate
End Sub
In this code, a webitem named OrderSearch has an HTML template file associated with it. When Visual Basic fires the Respond event for this webitem, it writes the contents of that template to the browser, which displays it to the end user.
Note The WriteTemplate method does not fire the Respond event if called from an event in another webitem. In order to keep the webclass code organized, you will often want to invoke the WriteTemplate method within a Respond event.
The WriteTemplate method can take one optional argument called Template that allows you to specify a different template file to return to the browser. This is helpful when you need to choose from a group of templates with common events. For example, you may have a set of news articles that are formatted similarly and contain a common series of hyperlinks and other tag elements, no matter what the story content. These articles are stored in template files available to a webclass. You can hook up the appropriate tag elements to events in your webclass, but then use the Template argument in a WriteTemplate call to specify the appropriate template file to retrieve for a user request.
For More Information See "Performing Text Replacements in a Webclass" for more information on the ProcessTag event and its processing as part of a Respond event. See "WriteTemplate Method" in the Language Reference for more information on using the WriteTemplate method and its arguments.
If you know HTML well, you can also write HTML line by line in your event procedures and send it to the browser using the Response object. This is a more complicated way of sending HTML than the WriteTemplate method, but it is useful in situations where you do not have a template to work with (such as if you are working with a custom webitem) or in which you want to generate a customized response.
The following sample shows how you use the Response object's Write method to send HTML to the browser. In this procedure, the webclass retrieves user information from a database and writes it to an HTML table:
Private Sub CustomerList_Respond()
With Response
'Write the HTML header information and start a table
.Write "<HTML>"
.Write "<BODY>"
.Write "<TABLE BORDER CELLSPACING=1 CELLPADDING=7>"
'Fill in cells until the end of file is reached in the recordset
Do While rs.EOF = False
'Write the user's name and address to a row in the table
.Write "<TR>"
.Write "<TD>" & rs("lastname") & "," & rs("firstname") & "</TD>"
.Write "<TD>" & rs("address") & "</TD>"
.Write "<TD>" & rs("city") & "</TD>"
.Write "<TD>" & rs("state") & "</TD>"
.Write "<TD>" & rs("zipcode") & "</TD>"
.Write "</TR>"
'Move to the next record in the recordset, then loop
rs.MoveNext
Loop
'End the table, then write closing tags for the page.
.Write "</TABLE>"
.Write "</BODY>"
.Write "</HTML>"
End With
End Sub
A few notes on using the Write method:
For More Information See "The Object Model for IIS Applications" for more information on using ASP objects in your webclass code.