by Noel Jerke
As a Visual Basic programmer, you know the Internet is going to play a big part in your future. And you've probably heard about VBScript programming on both the client side and the server side. With VBScript on the client side, you can interact with the user in a limited fashion on his desktop via the browser. For the server side, you have extended access to system resources to build true Web applications with Active Server Pages (ASP). But, in neither case do you have access to Visual Basic's powerful and rich environment.
But don't despair! You can use VB to extend your ASP applications by adding components similar to the ASP's standard components, such as the Response and Request objects. With VB 5.0, you can create an ActiveX DLL that will work as a COM object and can be called from your ASP applications. And, from your VB code, you can use the standard components provided with ASP to interact with your Web pages.
To begin, let's look at an overview of the events and objects available in VB for interacting with ASP pages. The first two important events are OnStartPage and OnEndPage. The OnStartPage event is called when the Web page that will use your component is started or requested. The OnEndPage event is called when the script in which the component was created finishes.
When a user requests a Web page in an ASP-based application, the server calls the OnStartPage method for all components on that page. This call occurs before the server processes the script. Via the OnStartPage method, you can use the ScriptingContext interface to retrieve pointers to the built-in object interfaces. Your component can then use the built-in object interface to access that object's collections, methods, and properties.
When the server finishes processing the script on an ASP page, it calls the OnEndPage method for all components on that page that have implemented the OnEndPage method. The server calls this method after processing the script on that page. You can use the OnEndPage method to free copies of interfaces stored by the OnStartPage method and any other resources that don't need to persist beyond script processing.
From our sample VB application, we can reference the built-in objects mentioned above, including the Request, Response, Server, Session, and Application objects. To have access to these objects and their methods and properties, you'll provide a scripting reference to work with the calling ASP Web page. To make this connection from your ActiveX DLL, you'll need to add a reference to the ASP type library in your VB Project. (Note that you must have either the Personal Web Server for Windows 95 or Internet Information Server 3.0 for Windows NT.)
Begin by creating an ActiveX DLL project in VB 5.0. Choose Project | References… from the main menu. In the resulting References dialog box, select the Active Server Pages type library.
In order to implement the OnStartPage event and create your own events, you'll create a VB class in the project. Select Project | Add Class Module and name the new class TextAreaConverter. Make sure its Instancing property is set to MultiUse so it can be instantiated multiple times on the server.
Now you're ready to create the public methods OnStartPage, shown in section 2 of Listing A. One argument will be passed, containing the scripting context that will allow you to interact with the ASP page where your object is created and used. Note that you immediately save that reference to a global variable, ASPsc, so it can be accessed globally in the class.
Listing A: ActiveX TextAreaConverter class
|
Section 1 |
|
Section 2 |
|
Section 3 |
|
Section 4 |
|
Section 5 |
To demonstrate how you can use the scripting context and ASP type library interface from this class, you'll implement a simple tool to convert text entered in a TextArea box with line feeds to text that can be displayed in HTML with <BR> tags. The first function in the class ConvertLineFeeds, shown in section 3 of Listing A, will convert all line feeds in a string into HTML <BR> tags. It does this by looping through the text string passed into the function and replacing the line feed and carriage return (ASCII 10 and 13) characters with the <BR> tag.
The second function you build into the class ConvertBreakTags, shown in section 4 of Listing A, will convert the HTML break tags found in the string into line feeds. It does this by looping through the text string passed into the function and searching for <BR> tags, then converting those tags into carriage returns (ASCII character 13).
Each function returns a string that has been appropriately converted. Also, to facilitate displaying data in a text area box on an HTML page, you'll build a CreateTextArea function, shown in section 5 of Listing A, that will create a text area and display the text passed into the subroutine.
In this function, you'll see how to use the scripting context to interface with the page directly. The first thing the subroutine does is retrieve a reference to the Response object available to us via the ASP type library, by referencing the scripting context, then dot (.) referencing the Response object.
Once you have a reference to the response object, you can begin writing to the calling page with the Response object. In this case, you're going to write beginning and ending HTML text area tags with the default text set to the value passed into the subroutine. In the same way you've used the Response object to interact with your ASP page, you can reference all the other ASP objects from your VB code—this is your key for integrating VB objects directly into ASP pages.
Now that you've created your object, you're ready to create a set of files and install them on your system. The best tool to use is the Application Setup Wizard that comes with VB. You can launch the wizard from your Start menu, as shown in Figure A. Follow the wizard's steps to create a standard set of installation files, then install the DLL on your system. Note that you'll want to set the properties for your VB project so that the application and project name are similar to or the same as TextAreaConverter.
Figure A
It's easiest to create your install files using the Application Setup Wizard.
Once you've installed the DLL on the system, you're ready to create Web pages that will use it. Listing B shows a Default.ASP page that will take in text from the Web page user. This text is entered in a standard HTML text area box that's created directly in HTML. The Text Area box is on an HTML form that will be submitted to the Convert.ASP page shown in Listing C. First, in this page you create an instance of our TextAreaConverter object via the VBScript CreateObject method. In this case, you store the reference to the object as TSC.
Listing B: Default.ASP Web page
<%@ LANGUAGE="VBSCRIPT" %><HTML>
<HEAD>
<TITLE>Visual Basic 5.0 Component Example</TITLE>
</HEAD>
<BODY BGCOLOR=WHITE>
Enter in the text you would like to have converted _
to be displayed as HTML:
<BR>
<i>Note you want to include several returns.</i><BR><BR>
<!-- Build the form to submit the entered data. -->
<form action="convert.asp" method="post">
<!--Create a text area box for entering the data. -->
<textarea name="SampleText"></textarea>
<BR><BR>
<!-- Build a submit input element -->
<input type="submit" value="Submit">
</form>
</BODY>
</HTML>
Listing C: Convert.ASP Web page
<HTML><BODY BGCOLOR=WHITE>
<B>Converted Text:<BR><BR></B>
<%
' Create our text area conversion object
Set TAC = _
Server.CreateObject("TextAreaConv.TextAreaConverter")
' Convert the text entered by the user to take _
‘ out the carriage returns and line feeds. _
‘ They will be converted into HTML <BR> tags.
Converted = TAC.ConvertLineFeeds(request("SampleText"))
' Write the converted text.
response.write(Converted)
%>
<BR><BR><HR>
<B>Non-Converted Text:<BR><BR></B>
<%
' Write the non-converted text.
response.write(request("SampleText"))
%>
<!-- Create a form to convert the text back. -->
<FORM action="ConvertBack.asp" method="post">
<!-- Store converted text in a hidden element -->
<input type="hidden" name="Converted" value="<%=Converted%>">
<!-- Create a submit element -->
<input type="submit" value="Click to Convert Back">
</form>
</BODY>
</HTML>
Now that you have the object, you're ready to begin using the methods you've created. Note that when the object was created, the OnStartPage event was called, and the scripting context for the page was passed in. Next, the text is retrieved from the form using the Request object. That text is passed into the ConvertLineFeeds method of your object, and the line feeds in the text are converted into HTML <BR> tags. The converted text is then displayed on the page. In the second half of the page, you display the text without converting it. Obviously, the carriage return and line feeds are still in the text, but HTML won't do any line feeds without the <BR> tags.
Also included on the page is a form that will pass the converted text onto another page, where it will be converted from formatting for HTML back to formatting for display in a text area input element. Listing D shows the code for this page. Note that once again you create an instance of your object. You then call the ConvertBreakTags method to convert the text out of HTML format. Finally, you use the CreateTextArea method and pass in the converted text to display it in an HTML text area. The text displays with the appropriate line breaks, while the final text area box with the unconverted text does not.
Listing D: ConvertBack.ASP Web page
<HTML><BODY BGCOLOR=WHITE>
<B>Converted Text:<BR><BR></B>
<%
' Create our text area conversion object
Set TAC = Server.CreateObject("TextAreaConv.TextAreaConverter")
' Convert the break tags into carriage returns
Converted = TAC.ConvertBreakTags(request("Converted"))
' Create a text box to display the text.
TAC.CreateTextArea(Converted)
%>
<BR><BR><HR>
<B>Non-Converted Text:<BR><BR></B>
<%
' Create a text area to display the non-converted text.
TAC.CreateTextArea(Request("Converted"))
%>
</BODY>
</HTML>
When the developers of the Active Server Pages technology decided to make the interface to the object library and model open and extensible for VB programmers, they knew they were providing a way for all of us to leverage our years of expertise and code. Now, through the use of standard class objects, we can begin placing the business logic programmed in VB right onto our Intranet/Internet Web sites.
As you can see with the scripting context and type library, you have the ability to interact directly with the user and the Web environment in the same way your ASP applications can by using the built-in ASP objects. This powerful combination will allow you to build Web applications that won't be limited to HTML and scripting language capabilities.
------------------------------
Noel Jerke is the director of online services for Judd's, Incorporated, a large magazine printer and Web-site builder. Noel has authored several books, including Visual Basic MultiMedia How-To, Visual Basic 5 Client/Server How-To, and VBScript Interactive Course. You can reach Noel via E-mail at noelj@judds.com or visit his Web site at http://www.activepubs.com.
-----------------------------
This article is reproduced from the December 1997 issue of Inside Visual Basic. Inside Visual Basic is an independently produced publication of The Cobb Group. No part of this article may be used or reproduced in any fashion (except in brief quotations used in critical articles and reviews) without prior consent of The Cobb Group. To contact The Cobb Group, please call (800) 223-8720 or (502) 493-3200.
Copyright © 1997 The Cobb Group, a division of Ziff-Davis Inc. The Cobb Group and The Cobb Group logo are trademarks of Ziff-Davis Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of Ziff-Davis is prohibited.