This article may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist. To maintain the flow of the article, we've left these URLs in the text, but disabled the links.
|
Visual Basic 6.0, You Won't Believe What Just Hit the Web
Joshua Trupin |
Our fearless technical editor risks life and limb to bring you a preview of DHTML editor and WebClasses, two Web-centric features planned for Visual Basic 6.0. |
Most programs have a definite life cycle. They're introduced with great fanfare, are given minor improvements from release to release, and gradually become long in the tooth. Newer, more innovative software is introduced, and the older product gradually loses its user base.
These patterns have never applied to Microsoft® Visual Basic®, a development package that has been ahead of the pack with every version that's been released. Recently, I got my hands on a beta version of Visual Basic 6.0, and it may just be the most impressive upgrade yet. Where previous versions have included support of OLE (now ActiveX®) controls, 32-bit programming, COM, and all the latest improvements in Windows®-based development, Visual Basic 6.0 goes one step further. The new product is one of the most appealing Internet development environments imaginable, for both client and server-side deployments. Before the beta police found me out and threatened to send me to beta prison if I discussed the entire package, I managed to get a few screen shots and play around with some code. Just remember, some of the stuff you read here could change before the final release. Stuff like that happens when you toy with the beta police. Visual Basic 6.0 contains faithful representations of two major Web design targets: the Internet Explorer Document Object Model and ISAPI. If you're targeting client-side implementation, you can generate real HTML files based on pure Dynamic HTML (DHTML). If you're working on a server project, Visual Basic lets you create server-side DLLs that are not only ISAPI-compatible, but give you access to all the standard ISAPI objects. Though it's easy to switch between these two mindsets, Visual Basic strictly segregates the functionality, as I'll explain later. While the client-side DHTML model is currently specific to Microsoft Internet Explorer, the server-side objects share the biggest advantage of ASP code: the output is completely cross-platform, cross-browser enabled.
The DHTML Page Designer
|
If that were all Visual Basic 6.0 provided, it would be enough to earn the Thumbs-Up, DHTML Editor Edition. However, the DHTML Page Designer has been packed with additional goodness that takes advantage of the way the Document Object Model previously encapsulated standard HTML tags. The Designer gives you a hierarchical display of your HTML document while it's being designed, even adjusting it dynamically as you move things around on the screen. It's really cool to copy and paste a page into the Designer and watch the Document tree automatically redisplay your content. Figure 1 shows a recent MIND Web page. I selected all and pasted it into the DHTML editor. Everything, including the animated GIFs, was immediately parsed, and I was up and editing as shown in Figure 2.
To understand how this all works, you need to have some knowledge of HTML as well as Visual Basic. In HTML, individual tags can take parameters. For instance, you can create a standard <BODY> tag to mark where the body of an HTML document begins (this is properly matched by a </BODY> tag, although browsers don't always enforce this). You're able to set parameters on this body tag and affect the document's background color, ID (unique name), and so on. By making a tag like
<BODY id="Body01" bgcolor="#FF0000">
for instance, you can indicate that the body of the document should be red (#FF0000 in RGB), and the element's ID should be Body01. Now that you've defined Body01, you can reference it in scripting code, access its properties, call its methods, and respond to its events.
WebClasses
Editing DHTML is the easy part. The real value of Visual Basic 6.0 lies in its ability to create fully functional ISAPI components that are then served to an end-surfer via ASP. You create one of these WebClasses about how you'd expect in Visual Basic. You create an application visually, write code that responds to certain of the object's events, debug it, and distribute it. Each of these activities merits some discussion.
A WebClass starts out as an empty IIS application named Project1. The name you give the application is registered as a virtual directory on your servereither IIS or Personal Web Server (PWS), depending on the Windows platform you've chosen. Visual Basic also generates WebClass1.
It's important to note that there's a one-to-one correspondence between WebClasses and ASP files, while a WebClass can contain any number of modules known as WebItems (see Figure 3). WebItems can contain either display code or response text, and have full access to all the global objects available to any ASP page: Application, BrowserType, Request, Respond, Server, and Session. To help you better visualize the overall structure of a Visual Basic-based IIS application, check out the diagram in Figure 4.
You can quickly get an idea of how easy it is to work with WebClasses by making your own Hello Web app. Just open a new project of type IIS Application. You'll get an object named WebClass1. I renamed mine HelloWeb. I also set the self-explanatory NameInURL property to HelloWeb. WebClasses have a Start event, which can be used to redirect forms to the browser or to do direct writes. Figure 5 shows how you can use the ISAPI Response object's Write method to return some simple content.
The first time you hit F5 to run a project, Visual Basic asks you what name you want for the virtual directory. I called mine HelloDir. This name can be changed at any time from your Web server management program. From then on, Visual Basic brings up your favorite browser when running the program (we at MIND recommend Internet Explorer, as others may cause the cake to fall), and navigates to the URL http://localhost/HelloDir/HelloWeb.ASP. You get, as you'd expect, the output shown in Figure 6. What does the code in the ASP look like? Look at Figure 7. You don't actually get any visible code. The class you've just created is a COM object that's called by the Web server. When you create HelloWeb.dll, you've effectively deployed this solution on your local Web site.
That was easy. But many applications will need to go further than just a bunch of Response.Write calls. For the vast majority of situations, you'll need to create WebItems as part of your application. There are two types of WebItems: HTML templates and custom WebItems (which don't provide any user interface).
Loyal readers (thanks, mom!) will remember an article I wrote in the June 1995 issue of MSJ that introduced a Bad Song database written in Delphi. Things have changed in the past three years. Users now expect you to serve up data flawlessly over the Web. Fortunately, data access in Visual Basic is easier than pie, so it's a perfect solution for server-side access of any ODBC-compliant source. For simplicity, I've stored the still-growing Bad Songs database in a Microsoft Access .mdb file. Instead of allowing only one comment per song, I decided that users might like to add their own comments as warranted. Users can also add songs, but not delete them. (Seems harsh, but it's a security measure made necessary by hordes of angry Little River Band fans trying to hack into the article.)
The first step of creating any good Web app is the design of its parts. Figure 8 shows the structure of the four pages in the Bad Songs 2000 (BS2K) application. Each page is a separate ASP page, so each one will have its own WebClass. Since they'll always have the same general layout, but different data, they're obvious candidates for HTML templates.
This actually proved to be a tricky concept, because
Visual Basic distinguishes between server and client projects. You can't mix and match effectively within a single projectfor instance, dragging and dropping DHTML pages from a project into the HTML template WebItems folder isn't doable. You can save a DHTML page as an HTML file (not as a Visual Basic .dsr file), then load it from the file into an HTML template, but users will generally design their HTML file in an editor like FrontPage® Express. When you do load an HTML file as a template, Visual
Basic immediately parses it and presents a list of tags that might require server-side interventionthings like hyperlinks, images, and forms.
This caused a brief conceptual roadblock for me, because I expected to see a list of all my WebItem's tags available and ready for scripting. Again, this doesn't happen because client and server code are kept strictly separate. If you want to set events on HTML client tags, you can do that from a DHTML page in a client project. Since a click on an edit field, for example, doesn't ever get reported to the server, there's no
reason to provide template code for it in a WebClass. You
only get certain obvious events to handle in your WebItemthings like submitted forms. I designed four pages that looked pretty similar, and loaded them into the appropriate WebClasses.
As indicated earlier, the ASP code that fronts a WebClass first calls the WebClass_Start routine, so you always want to put some code in here to return the appropriate HTML. You can accomplish this in code with two valuable commands. The NextItem global lets you indicate the next WebItem that will have control over program execution. If you want to start using the WCSongs WebItem, you simply need to say:
Set NextItem = WCSongs
Setting NextItem fires the object's Respond event. (Respond is fired as default handler code any time a WebItem object is activated by user request and the element chosen doesn't directly correspond to an event.) If you've created an HTML template and just want to stream that back to the user's browser, you can use the template's WriteTemplate method:
tplHeader.WriteTemplate
I went one step further, in fact, by combining a header template, a footer template, and custom Response.Write code in the WCSongs WebClass_Start. (The code for the WCSongs WebClass is shown in Figure 9. As with any beta, this should be considered subject to change before the final release.)
<HTML><BODY>
<FONT size=4><WC@titletag>This is an internal Webitem.</WC@titletag>.</FONT>
</BODY></HTML>
and you've defined the TagPrefix for this HTML template to be WC@, the information between the <WC@titletag> and </WC@titletag> can be processed in the ProcessTag event:
Private Sub MINDPg_ProcessTag(ByVal TagName As String, _
TagContents As String, SendTags As Boolean)
Select Case TagName
Case "WC@titletag"
TagContents = "Cool!"
End Select
End Sub
I'm not sure why it doesn't strip off the prefix before passing it in as the TagName, but I'm just the messenger here. When the HTML in the previous sample actually shows up in the browser, it'll look like this:
<HTML><BODY>
<FONT size=4><WC@titletag>Cool!</WC@titletag>.</FONT>
</BODY></HTML>
Cool! Of course, you'll still want to do a bit more. Receiving Web-based input and acting on it might be nice. Remember that you can't get to individual client-side events from a WebClass, but you can intercept forms posted from the template's HTML code. Your <FORM> tag's method must be set to POST. In fact, if you try to load a GET form as a template, it will be converted to a POST. The only time it makes sense to use a QueryString is when you've hardcoded an URL as a target. In the WebClass viewer (see Figure 10), you can right-click an HTML template and map the server-touching tags to a custom event or a WebItem. This is also a little bit tricky. If you have a Form named Form1 in a template named tplSample, you have to specifically perform a mapping before the function tplSample_Form1 has any meaning to the WebClass. If you delete and recreate a WebItem for some reason, make sure to look out for this potential gotcha.
When you're executing code from within a WebItem,
you have full access to the standard ASP objectsremember, your code is really an ISAPI DLL. These are actually quite easy to use. Figure 11 shows a list of these objects and their members. When you enter a WebClass procedure that's been connected to a client-side form, the Request.Form object will be filled with a list of values, one per form component.
Take, for example, the code I've designed to let users add songs to the Bad Songs database. This WebClass, WCAddSong, corresponds to the template page shown in Figure 12 (image), Figure 13 (form code) and Figure 14 (the actual WCAddSong Visual Basic code).
This page has three buttons: Add, Clear, and Back. Add is a standard form submit button as seen on numerous Web pages. The Clear button is a standard reset buttonthis will clear the fields of the form that contains it. The third button, Back, is a plain old command button. Just to be tricky, I've added some DHTML into this template. When the Back button is clicked, it triggers the btnBack_
onclick event. In this VBScript subroutine, I call window.history.back, simulating the Back button on the browser. Sometimes you just have to bite the bullet and use client-side code, even if you're primarily a server-side process.
When the Add button is clicked, this form is submitted to the WebClass. There are four elements in this formedtSong, edtArtist, edtYear, and edtComment. These are concatenated into a single form string that can be accessed in the WebClass as Request.Form. A typical string might look like this:
edtSong=Lovin%27+You&edtArtist=Minnie+Ripperton&edtYear=1975&edtComment=
Only+audible+to+certain+breeds+of+dog&btnAdd=Add%21
Fortunately, you can pass an individual parameter name to Request.Form, so you don't have to do your own parsing. If you say Request.Form("edtArtist"), you'll get the value "Minnie Ripperton."
Response.Redirect "WCSongs.ASP"
Needless to say (but I'll say it anyway), a basic knowledge of ASP is helpful when using WebClasses, but IntelliSense makes things a whole lot easier if you can remember the basic ASP objects. Since you have the ability to set breakpoints in the middle of code and dive into the complete Visual
Basic IDE, you can quickly get a handle on what's available to you in a WebClass. Just stick the objects in a Watch window and you can check out any of their property values whenever you want. You can do anything you want in an Active Server Page, and you can do it in the comfortable Visual Basic environment.
Summary
Visual Basic 6.0 is scheduled for release later in 1998. (Check back often for details.) I've only scratched the surface of Visual Basic 6.0 and its new features. However, from the point of view of an interactive developer, these two conceptsthe DHTML editor and WebClassesare by far the most important and welcome changes. The ease and power of Visual Basic has once again been adapted to smooth the learning curve of an important programming technology, and that's an innovation everyone can love.
From the August 1998 issue of Microsoft Interactive Developer.