Page #34 (Chapter 4 - Introduction to WebClasses)

Chapter 4 - Introduction to WebClasses

Visual Basic Developers Guide to ASP and IIS
A. Russell Jones
  Copyright 1999 SYBEX Inc.

Writing HTML without Templates
Despite their availability, you don't have to use an HTML template to create an IIS project. HTML templates are typically more convenient for complex pages and for pages where the "look" may change often. Sometimes, it's simpler to write the HTML from inside the WebClass.
Here's a simple example. Suppose you want to display a table from a database. Start a new IIS project in VB. Right-click the project and select Properties. Rename the project showTable. Right-click the WebClass and rename it tableViewer. Save the project.
  Note When you first run an IIS project, VB will create a virtual Web for you. If you don't save the project, VB creates a source folder for the virtual Web in your Temp directory. If you save the project, it creates the source folder for the virtual Web as a subdirectory of the project folder.
Double-click the WebClass Designer. You'll see an Explorer-type view with two folders: one titled HTML Template WebItems and the other titled Custom Web-Items. Right-click the Custom WebItems folder and select Add Custom WebItem. Title the new WebItem showTable. Your designer window should now look like Figure 4.3.
Double-click the showTable custom WebItem and place the code in Listing 4.2 into the Respond event. Note that this project uses the Microsoft pubs database that comes with SQL Server. If you do not have SQL Server, you can download a Microsoft Access database containing the tables of the pubs database from the Sybex Web site.
  Note To download code, navigate to http://www.sybex.com. Click Catalog and search for this book's title. Click the Downloads button and accept the licensing agreement. Accepting the agreement grants you access to the downloads page for the book.
Listing 4.2: ShowTable_Respond Event
Private Sub showTable_Respond()
    Dim conn As connection
    Dim R As Recordset
    Dim F As Field
    Dim s As String
    Set conn = New connection
    conn.ConnectionString = "pubs"
    conn.Mode = adModeRead
    conn.Open
    Set R = conn.Execute("authors", , adCmdTable)
    s = "<html><head><title>Pubs Database–" & _
         "Authors Table</title></head>"
    s = s & "<BODY>"
    s = s & "<table align=""center"" width=""95%"" & _
         "border=""1"">"
    s = s & "<tr>"
    For Each F In R.Fields
        s = s & "<td align=""center""><b>" & F.Name _
            & "</b></td>"
    Next
    s = s & "</tr>"
    While Not R.EOF
        s = s & "<tr>"
        For Each F In R.Fields
            If IsNull(F.Value) Then
                s = s & "<td> </td>"
            Else
                s = s & "<td>" & F.Value & "</td>"
            End If
        Next
        s = s & "</tr>"
        R.MoveNext
    Wend
    R.Close
    Set R = Nothing
    conn.Close
    Set conn = Nothing
    s = s & "</table></body></html>"
    Response.Write s
End Sub
You're almost done. You need to tell the WebClass which item to show first. Find the Start event for the WebClass and place this code into the event, replacing the code that the IIS Application Wizard automatically puts there:
Private Sub WebClass_Start()  
   Set NextItem = showTable
End Sub
Now run the project. VB displays a dialog box stating that it is going to create a virtual Web for your project. Accept the default. The browser appears and should look similar to Figure 4.4.



Visual Basic Developer[ap]s Guide to ASP and IIS
Visual Basic Developer[ap]s Guide to ASP and IIS
ISBN: 782125573
EAN: N/A
Year: 2005
Pages: 98

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net