Using VB6's new WebClass technology makes it easy to create flexible and powerful Internet-based applications. Earlier this year, we showed you how to write a registration application that allows users to visit your page and register for a software product. .["Create a User Registration Class," VBPJ April 1998 and "Add E-Mail Registration to your Server," VBPJ May 1998]. After registration, the application will send a sequence to the user via e-mail number, and a connection to the Web exists to continue the registration process. The VB5 application I showed you earlier created an ActiveX DLL that runs on the Web server and a DLL that determines the user's status and calls the DLL appropriately. Corresponding ASP script. (Active Server Page). Now VB6 provides a new means for writing this type of application, using a new technology - WebClass.
In short, WebClass is ActiveX running on the Web server DLL. It allows the hyperlink of the HTML page on the client's browser to activate events in the server DLL. The programming mode of WebClass is similar to the traditional VB mode - the difference is that in VB, the form contains controls, while the WebClass application The program is a Web page that contains controls. On the server side, for developers, he knows the complete VB event model, so that highly interactive Web development can be performed. In this column, we will discuss how to create a simple WebClass application to show you how simple this is.
To create a simple WebClass application, you should start VB6 and select the IIS application in the New Project dialog box. Name the project SimpleReg in the Properties window. Double-click the project WebClass Designer in the browser. (You can see the Properties window and Project Browser through the View menu). In the Properties window, name the WebClass wcSimple and enter SimpleReg in the NameURL property. This will be created when compiling the ActiveX DLL A startup file named SimpleReg.asp. Then save the project.
A WebClass application displays HTML in the user's browser by using an HTML template. Since VB6 does not include an HTML editor, you must create an HTML template outside of VB6 (VB6's DHTML designer has nothing to do with WebClass at all). You can use any HTML editor, but since Visual Studio includes Visual InterDev (VID) 6.0, VID is a reasonable choice.
Set Visual Studio as the default HTML editor for Visual Basic in the Tools|Options menu of VB6. In the Advanced label , fill in the following Visual InterDev path - C:Promram FilesMicrosoft Visual StudioCommonIDE|IDE98Devenv.exe into the external HTML edit box. You will notify Visual InterDev to make a template file for your project For example: if your template is called WebPage.htm, then InterDev creates a copy named Web1Page.htm. If you use Notepad or other editors to create your template, please take this into consideration.
In the routine code , the SimpleReg.htm file is a template created by Visual InterDev. In the left pane of the WebClass designer, select the HTML Template WebItem folder. You can add the SimpleReg template to the project as follows: click the AddHTML Template WebItem button and click on Select the SimpleReg.htm file in the file selection dialog box (see Figure 1). Name the new WebItem tplSimple. Double-click tplSimple to view the code about WebClass. Find the WebClass_Start process and replace the original generated code with the following code to display the template when loading WebClass :
Private Sub WebClass_Start()
'Show the main HTML template www.downcodes.com
Session("Title") = "Enter Your" & "Registration Information"
tplSimple.WriteTemplate
End Sub
WebClass_Start process is similar to the Form_Load process of a traditional VB application. Run the program and watch the HTML page displayed by your browser. Exit the browser and terminate the VB application. (This is a normal shutdown during debugging and development. WebClass method).
In the WebClass Designer, right-click on the tplSimple WebItem and select Edit HTML Template from the context menu. You will see the template page in your default HTML editor. Reading the source code, you can see This is a simple page consisting of three special parts called tags, in the following format:
< WC@TAGTITLE>Title</WC@TAGTITLE >
The text part of these tags ("Title") is changed by you at runtime The code added to WebClass is replaced. When the WriteTemplate method is executed, it calls a special procedure called ProcessTag once for each tag found in the HTML template. Back in VB, double-click tplSimple WebItem to display the code window. In the code window Select the tpSimple_ProcessTag process in the upper combo box and add the following code:
Dim sHTM As String
Select Case TagName
Case " WC@TAGTITLE "
TagContents =Session("Title")
Case " WC@TAGMAIL "
sHTM = "Enter Email Address:<br>"
sHTM = sHTM & "<input type= 'text'"
sHTM = sHTM & " name= 'Email'><br>"
TagContents = sHTM
Case " WC@TAGNAME "
sHTM = "Name:<br>"
sHTM = sHTM & "<input type= 'text'"
sHTM = sHTM & " name= 'Name'><br>"
TagContents = sHTM
End Select
returns the HTML code in the TagContent parameter to replace the tag. Set a breakpoint in tplSimple. Add WriteTemple to the WebClass_Start procedure and run your application. At the breakpoint, use F8 to step through the project to see The order of events. You will see that the ProcessTag procedure is called three times - once for each tag in the template. The really cool thing is: at runtime the procedure will use HTML that will generate a text box in the browser. Code replaces the E-mail and Name tags.
Being able to interactively debug your server-side event code is one of the very powerful features of developing WebClass applications with VB. In VB5 Web development, VBScript in ASP scripts is completed A lot of work. You can't debug VBScript interactively in ASP scripts - the only feasible way unfortunately is error and try again. However in VB6, all server-side code runs in WebClass and you can use VB's excellent design environment for interactive debugging.
Connecting an event
The next step in this simple application is to add event handling code for the Register Me button on the form. Return to VB's WebClass designer and click on tplSimple WebItem. In the right pane, you will see a list of HTML members in the page, and you can associate events with them. Right-click the Form1 tag and select the Connect to Custom Event menu item, and notice that next to Form1 Event name in the Target column. Take a quick look at the HTML template (right-click on tplSimple and select Edit HTML Template). Find the members of the form. They now look like this.
<FORM method = 'post' action = SimpleReg.ASP? WCI=tplSimple_
&WCE=RegisterMe&WCU>
SimpleReg.asp is a special ActiveXDLL startup script. The value after the question mark indicates that WebClass Item (WCI) is tplSimple, WebClass Event (WCE) is RegisterMe, and WebClass URLData (WCU) is empty. When the user When the Register Me button is clicked in the browser, this line will activate the tplSimple_RegisterMe button in the ActiveX server DLL. In this way, the form's activity is tied to the server-side event, but you can set a hyperlink in the browser to activate Events in the server. WebClasses work like magic for Web application development - they extend VB's event-driven programming to browser-based applications.
Double-click the RegisterMe event in the left pane of the VB Designer to display the code window. Add the following code to the event:
If Len(Request.Form("Email")) = 0 Then
Session("Titile") = "Please" & "Enter an Email Address!"
Session("Email") = ""
Session("Name") = ""
Else
Session("Title") = "Here is" & "your Entry!"
Session("Email") = Request.Form("Email")
Session("Name") = Reuquest.Form("Name")
End If
TplSimple.WriteTemplate
Request.Form is a standard way of returning data from a browser page. It sets some Session variables and redisplays the same HTML page. Set a breakpoint at the If statement and run the project. Enter an email Address and name, click the Register Me button. You will see that the title has been refreshed, but the text box is empty. This is because you did not enter the corresponding values for them when writing the template. Terminate the project and change the tplSimple_ProcessTag process ( Download Listing 1 from DevX; see the Download Free Code box for details). Assign a value to the text box based on the Session variable. Run the project and see what happens.
Use ADO for data access
.Finally, add a way to store and store the sum from the database. Method to obtain records. For simplicity, we use Microsoft Acess 97 as our database. Create a new database with Acess in your source directory and name it Register.mdb. Add a table with two fields, respectively Email and Name, and save them to disk. Select OK when Acess prompts you to create a unique index field, and place a copy of Register.mdb in the root directory of your C drive.
In the project, add a Microsoft ActiveX Data Object Library references, you can find them under Project|Reference.... You can use the ADO library version 1.5 or 2.0. If you don't have the ADO library installed, you can download it from Microsoft's site: http://www.microsoft.com/data Download the MDAC2.0 installation toolkit from /ado. Change the code in the tplSimple.RegisterMe process (Download Listing 2 from DevX; see the Download Free Code box for details).
This code stores the data in the form into the Session. variable, then open the ADO recordset and look for the email address (Download Listing 3 from DevX).
If found, the code will return the record. If not found, it will add a new record. The function sets the connection string:
private Function Connect() As String
'Return an ADO connection string
Const kDB = "DBQ=c:register.mdb;"
Const kDrv = "Drive={Microsoft" & "Acess Driver (*.mdb)}"
Connect = kDB & kDrv
End Function
Set a breakpoint in tplSimple_RegisterMe, run the program, and see how the data access code works. This simple example shows you how simple programming in ADO is. Remember to configure it in your company Any "real" application should include error handling. Generally speaking, you will also use registration to save variables such as connection string information and you will need to provide a way to refresh records and delete unnecessary records.
When When you compile the project, two things will happen. The first thing is: like any ActiveX project in VB, an ActiveX DLL record will be generated on the development machine. The second thing is: a WebClass will be generated. The special ASP startup file named by the name specified by the NameInURL attribute. In the example, the name of the file is: SimpleReg.asp. Look at the script in Notepad. When the user enters the URL of the file in the browser, IIS creates an instance of SimpleReg.wcSimple WebClass. Microsoft recommends that you do not change this ASP script under any circumstances.
From the user's perspective, registering the WebClass version of the application is similar to the series shown in May 1998. The code for this series It is indeed similar to the series shown earlier, except that now all the code is included in the WebClass, and the launched ASP file does not include any user information.
Notes
Comparison of the application before and after , you should have a consistent understanding of how WebClass works. However, you may still be reluctant to immediately convert your Web application to a WebClass project. Converting an application takes considerable time, but you should Do this because there are real benefits to doing so. For example: WebClass allows you to create or extend applications in ways that were not possible before.
In short, Microsoft provides us with a set of tools for developing Internet applications. Of course, it is developed with VB5 Web applications are also possible, but you'll have to use a lot of little programming tricks to make the ASP script interact with the VB DLL correctly. Without the limitations of VBScript, there is no lack of a true event-driven programming environment. WebClass gives VB brings full functionality to web development - and it's a good thing.