WebClasses makes registration easy

Source: Internet
Author: User
Tags empty error handling extend html page iis connect tagname visual studio

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 registered application that allows users to visit your page and register a software PRODUCT. ["Create a User registration Class," VBPJ April 1998 and "Add e-mail registration to your Server," VBPJ May 1998]. After registering, the application will pass the E-mai L send a serial number to the user, and there is also a connection to the Web to continue the registration process. Earlier demonstration of the VB5 application creates an ActiveX DLL running on the Web server and a corresponding ASP script that measures the state of the user and invokes the DLL appropriately. (Active Server Page). VB6 now provides a new tool for writing this type of application, using both new technology-webclass.

In short, WebClass is an ActiveX DLL running on a Web server. It allows hyperlinks in the HTML page on the client's browser to activate events in the server DLL. The WebClass programming pattern is similar to the traditional VB mode-the difference is that In VB, a form contains controls, and a WebClass application is a Web page containing control. On the server side, for developers, he knows the complete VB event model, which allows for highly interactive web development. In this column, We'll discuss how to create a simple WebClass application to show you how simple it is.

To create a simple WebClass application, you should start the VB6 and select the IIS application in the New Project dialog box. In the Properties window, name the project Simplereg. Double-click the WebClass Designer in the engineering explorer. (You can see the Properties window and engineering Explorer through the View menu). In the Properties window, the named WebClass is Wcsimple and the Simplereg is entered in the Nameurl property. This will compile the ActiveX DLL, a startup file named Simplereg.asp is created. Then save the project.
An WebClass application displays HTML on 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 is completely unrelated to WebClass). You can use any HTML editor, but because Visual Studio includes Visual InterDev (VID) 6.0, VID is a reasonable choice.

In the VB6 of tools| The Options menu sets visual Staudio as the default HTML editor for Visual Basic. On advanced label, the following Visual InterDev path-c:\promram Files\Microsoft Visual studio\common\ide| Ide98\devenv.exe fill in an external HTML edit box. You will notify Visual InterDev to make a copy of the template file for your project. such as: Your template is called webpage.htm, That InterDev create a copy called Web1page.htm. Consider this when you create your template with Notepad or another editor.

In routine code, the simplereg.htm file is a template created by Visual InterDev. In the left pane of the WebClass designer, select HTML Template WebItem folder. You can add a Simplereg template to your project by clicking the addhtml Template webitem key and selecting Simplereg.htm file in the File Selection dialog box (see Figure 1). Name the new WebItem as Tplsimple. Double-click Tplsimple to view code about WebClass. Locate the Webclass_start process and replace the code that was originally generated with the following code to display the template when the WebClass is loaded:

Private Sub Webclass_start ()
' Show the main HTML template www.knowsky.com
Session ("Title") = "Enter Your" & "Registration Information"
Tplsimple.writetemplate
End Sub

The 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 the normal way to turn off WebClass during the debugging development process).

In the WebClass designer, right-click 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 that this is a simple page with three special parts called tags, The format is as follows:

<titleWc@tagtitle>title</wc@tagtitle>

The text portion of these tags ("Title") is replaced by the code you add to the WebClass at run time. When the Wrietetemplate method is executed, it invokes a special procedure called ProcessTag for each label found in the HTML template. Back to VB, Double-click Tplsimple WebItem to display the Code window. Select the Tpsimple_processtag procedure in the combo box at the top of the code window 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

Return the HTML code in the Tagcontent parameter to replace the label. Set breakpoints in Tplsimple. Writetemple add to the Webclass_start process and run your application. At the breakpoint, Use the F8 to step through the project to see the order in which the events occur. You will see that the ProcessTag process was invoked three times-once for each label in the template. The real cool thing is that the process will replace the e-mail and name tags with HTML code that generates text boxes in the browser.

The ability 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, The VBScript in the ASP script does a lot of work. You can't debug interactively in an ASP script vbscript-the only possible unfortunate way is error, try again. However, in VB6, All server-side code is run in WebClass and you can debug interactively with VB's excellent design environment.

Connect an Event

The next step is to add event handling code for the Register Me button in the form in this simple application. Back to the VB WebClass Designer, click Tplsimple WebItem. In the right pane, you'll see a list of HTML members on the page, You can relate the events to them. Right-click the Form1 tab and select the Connect to Custom event menu item and note the name of the event in the target column next to Form1. Quick look at the HTML template ( Right-click Tplsimple and choose Edit HTML Template. Find the members of the form, they are now 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 a Tplsimple,webclass event (WCE) Registerme,webclass Urldata ( WCU) is empty. When the user clicks the Register me button in the browser, this line activates the Tplsimple_registerme button in the ActiveX Server DLL. In this way, the activity of the form is associated with the server-side event, But you can set up hyperlinks in your browser to activate events in the server. WebClass the development of Web applications as magic-they extend VB 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 and 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 to return data from a browser page. It sets some session variables and displays the same HTML page again. Set a breakpoint at the if Declaration and run the project. Enter an email address and name and click Register The Me button. You will see that the title is refreshed, but the text box is empty because you did not enter a value for the template when you wrote it. Terminate the project and change the Tplsimple_processtag process (download listing 1 from DevX; for details, See Download free Code box). Assigns a value to a text box based on the session variable. Run the project to see what will happen.

Using ADO for data access

Finally, add a way to store and retrieve records from the database. For simplicity, we use Microsoft Acess 97 as our database. In your source directory, create a new database with acess and name it register.mdb. Add a table with two domains. They are email and name, side by side. When acess prompts you to create a unique index field, select OK to place a copy of Register.mdb under the root directory of your C drive.

In the project, add a reference to the Microsoft ActiveX Data Object Library, where you can project| Reference find .... You can use the ADO library version 1.5 or 2.0. If you don't have the ADO library installed, you can get from Microsoft's site: http://www.microsoft.com/data/ ADO Downloads the MDAC2.0 installation kit. Change the code in the Tplsimple.registerme process (download listing 2 from DevX; For details, see the Download free Code box).

The code stores the data in the form in the session variable, and then opens the ADO recordset and looks for an email address (download listing 3 from DEVX).

If found, the code returns a record. If it is not found, it adds a new record. The function sets the connection string:

Private Function Connect () as String
' Returns 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 and run the program to see how the data access code works. This simple example shows you how easy it is to program in ADO. Remember any "real" that is configured in your company Applications should include error handling. In general, you will also use registers to hold variables such as connection string information and you will need to provide a way to refresh records and delete unwanted records.

When you compile the project, two things will happen. The first thing is that, as with any ActiveX project in VB, an ActiveX DLL record will be generated on the development machine. The second thing is: Produces a special ASP startup file named after the WebClass Nameinurl attribute. In a routine, 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 the Simplereg.wcsimple WebClass. Microsoft recommends that you do not change the ASP script anyway.

From the user's point of view, the WebClass version of the registered application is similar to the one shown in May 1998. The code for this series is indeed similar to the one shown earlier, except that all the code is now included in the WebClass, And the ASP file that is started does not include any user information.

Attention matters

By comparing the applications before and after, you should have a consistent understanding of how WebClass works. However, you may still be unwilling to turn your Web application into a WebClass project at once. It takes a lot of time to transform an application, but you should do it, Because there are real benefits in doing so. For example: WebClass allows you to create or extend an application in a way that you didn't have before.

All in all, Microsoft provides us with a set of tools for developing Internet applications. Of course, it is also possible to develop Web applications with VB5, but you have to use a lot of small programming techniques to make ASP scripts interact with VB DLLs correctly. Out of the confines of VBScript, There is no such thing as a lack of real event-driven programming environments. WebClass gives VB the full functionality of web development-it's really a good thing.

Turn from: Dynamic Network production guide www.knowsky.com

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.