Begin creating the sample Web UI by following these steps:
Start VB and create a new ActiveX DLL project.
From the Project menu, choose References and check "Microsoft Active Server Pages Object Library." This object library is installed on your PC when you install IIS or PWS. Note that it is possible to use this technique without this ASP object library, but it uses late-binding, which is not desirable.
Give your component a nicer name than Project1 by clicking Properties under the Project menu. Type PublishersWebUI for the Project Name and Project Description.
Change the name of the default class (Class1) to clsWebApp.
Add a new (code) module to the project, and change its name to basGlobal.
In basGlobal, make the following declarations:
- In clsWebApp, type the code for the OnStartPage events as follows:
Code a public sub procedure in clsWebApp to act as a method that can be called from your ASP code:
And now, run your project. When you run an ActiveX DLL from inside the VB IDE, VB temporarily registers the component and its public classes, and points the registry back to the VB IDE. When another application creates an instance of a class that your application exposes, the source code from the class gets interpreted and run inside the VB IDE. When you stop the application, VB automatically unregisters your component. This behavior is great because it enables you to use all of VB's interactive debugging tools, just as you do when developing Windows apps.
At this point, your component is running and waiting for something to invoke it. To pass control from IIS (or PWS) to your component, you'll need to create one ASP file that creates an instance of the clsWebApp class and calls its WebMain method. (I know I said that this technique is scriptless, and now I'm telling you to create a script file, but this file is so tiny that I don't think it really counts.) Use Notepad to create a file named PubWebUI.asp, and type its contents as follows:
- Finally, run your Web browser, and type the URL for the PubWebUI.asp script file (for example, http://localhost/pubwebui.asp). If everything works as planned, you should see the "Hello World." text displayed in your browser window.
Public gAspAppl As Application Public gAspReq As Request Public gAspResp As Response Public gAspServer As Server Public gAspSession As Session
Public Sub OnStartPage(ByRef rScriptingcontext As ScriptingContext) Set gAspAppl = rScriptingcontext.Application Set gAspReq = rScriptingcontext.Request Set gAspResp = rScriptingcontext.Response Set gAspServer = rScriptingcontext.Server Set gAspSession = rScriptingcontext.Session End Sub
If you expose this OnStartPage method, it is called automatically when your class is instantiated from ASP code. Think of it as being similar to a Form_Load event. I stumbled on this quite by accident one day while surfing the net. I don't know where it came from, but it likely has something to do with WebClasses. In any event, it allows our code to grab a reference to the intrinsic ASP objects. As mentioned earlier, there are other ways to get these references, but I like this one best.
Public Sub WebMain() Call gAspResp.Write("Hello World.") End Sub
<% Dim mWebUI Set mWebUI = Server.CreateObject("PublishersWebUI.clsWebApp") Call mWebUI.WebMain %>Or, if you're feeling particularly clever, use this one-line version:
<% Call Server.CreateObject("PublishersWebUI.clsWebApp").WebMain %>sure that you save this script file to somewhere that is reachable by your Web browser, such as: c:\inetpub\wwwroot\PubWebUI.asp.
The sequence of the previous processing steps goes something like this: When you type the URL of your script, the Web browser sends a request for this file to the Web server running on your machine. The Web server reads the script file and, because it has an ASP file type, passes the request to the ASP and script-parsing components. When your script's CreateObject command is executed, the registry is searched for a component named PublishersWebUI (which we know points back to the VB IDE). Next, an instance of clsWebApp is created, and because we exposed a method named OnStartPage, it gets automatically called and is passed a reference to the scripting context. Our code then grabs a reference to the intrinsic ASP objects and saves these references in the global variables for later use. Finally, the class's WebMain method is called, which then uses the ASP Response object's Write method to send some simple text back to the browser.
Because we grabbed a reference to all of the intrinsic ASP objects, the VB code is free to use any property or method of these objects, just as VBScript code in an ASP page might. For example, our code can get the contents of form controls, obtain query string parameters, and use session variables.