Build Interactive Web Pages with VB 6.0's IIS Applications Capabilityby Noel Jerke Visual Basic 6.0 brings new capabilities to the table for Internet development. One of these is the ability to create Internet Information Server (IIS) applications. Such applications can run in a Web browser via Internet Information Server. Instead of your application being form-based and running on the client machine, the application runs on the Web server and sends out standard HTML to the Web browser over your Web site. In this article, we'll show you how to build IIS applications.
Creating the project In this article's example, you're going to build a popular feature of many Web sites: a guest register. To get started, launch Visual Basic 6.0 and start a new IIS application. The first thing you'll see is the new Web class designer, as shown in Figure A. In the Web class designer, you'll import HTML pages that will be the interface for the programthese Web pages replace the traditional form-based interface.
Figure A: Visual Basic 6.0 includes the new Web class designer. Listing A contains the HTML code for the GuestRegister template. Note that there are no HTML text-element fields for data entry; however, you can see custom Web tags like @firstname. These tags will be replaced in the IIS application with input elements for data entry. Also, note that the form for submitting the guest register is created on the page. And, there's no action set for the page's form to submit tothis will be defined in your IIS application. Listing A: HTML code for the guest register
Create the file as shown in Listing A. Then, right-click on HTML Template WebItems in the Web class designer and import the template. Be sure to name the template GuestRegister once you've imported it. Now, you're ready to enter some programming code behind the template. First, in the Web class interface, you need to define a custom Web event for your form submission. To do this, double-click on the Form1 tag in the right pane of the Web class designer to create a custom form1 event. (You'll add code to this event a little later.) The application program must specify which item to display first when the user accesses the application. In the WebClass_Start event, you specify the next item as your GuestRegister template by setting the NextItem property of the Web class. When that's done, the Respond event of the GuestRegister template is fired off. In that event, you use the WriteTemplate method of the GuestRegister template to write the page out to the browser. When the template is written out to the browser, the program encounters your wc@ custom tags on the page. The program then calls the ProcessTag event of GuestRegister with the tagname passed in as a parameter. You then have the opportunity to return HTML code to the application for insertion into the document where the custom tag is located. Our code has a Select Case statement that will set the TagContents parameter of the event based on the tag. When this is done, the TagContents are written out in the page sent to the browser. For each of the tag contents, you build an HTML input element for the user to enter the data. You also set the value of each element to the current corresponding global variable (that is, chrFirstName). When you validate the user data in the form1 event, the data is stored globally so you can redisplay if it has been incorrectly entered. Figure B shows the form at runtime.
Figure B: Create this Guest Register data-entry form. Note that the wc@error tag is processed first. For this tag, you check to see if there's any data in the strError variable to be displayed. The strError variable will contain data if the data entered by the user isn't correctyou want to at least set the values of the input elements to contain their original typed data, so the user doesn't have to re-key it. Figure C shows the form with error notifications at the top of the page.
Figure C: The Guest Register indicates data-entry errors. Now that you've taken care of displaying the form for input, you're ready to process the data when the user clicks the Submit button. To add code to the form1 event, double-click on the form1 listing in the left pane of the designer. Listing B shows the code for the IIS application, including the form1 event code. Basically, when the form is submitted, you'll verify that the user properly entered data in all the fields. Listing B: The IIS application code
All fields are validated except the phone and comments fields. Note that you check to ensure that a proper E-mail address with the @ sign has been entered. If any fields are blank, you build an error string that will display to the user. The data is read from the submitted form using the Active Server Pages Request object and specifying the text-element field name. If any field has an error, the strError global variable is set with the appropriate error message. If all the data that the user enters is correct, then you're ready to write the data to the database. Listing C shows the SQL script for creating the databasea simple one-table structure with several fields for storing the data elements. A unique ID field will be auto-incremented with each insert. Listing C: SQL script to create the guest-register database
The InsertGuestReg subroutine handles opening and ADO database connection, building a SQL query for inserting the data, and executing the query. This function is called if no errors are found in the Form1 event. Once the data is inserted, the Response object writes a thank-you note to the user, as shown in Figure D. Note that this page is very simple; no links lead from it. Obviously, in a live situation, the page would be more dynamic. You'd do so simply by writing out additional HTML to the page along with appropriate document tagging structure (such as , and so on).
Figure D: The program displays a thank-you message after a successful guest register submission. That's it. From this simple example, you can see the combination of the Web interface with traditional back-end Visual Basic programming. As with Active Server Pages applications, you still have to keep state through the use of session variables, hidden variables, and URL query strings. There's an option for the IIS application to keep state, but be careful of memory issues that will increase the size of the application and decrease its scalability. ConclusionIIS applications combine the best of traditional Visual Basic programming with the power of the Web. You can use all the traditional programming methodssuch as object-oriented programming, data abstraction, and so onto build your Web interface. And, you can leverage any business logic on the Web you may already have in VB for a client/server context. For the interface, the developer now simply needs to think in terms of Web pages, rather than VB forms. If you need to deploy intranet, Internet, and/or extranet applications, you now have the flexible power of VB at your disposal.
|
|||
Copyright © 1998, ZD Inc. All rights reserved. ZD Journals and the ZD Journals logo are trademarks of ZD Inc. Reproduction in whole or in part in any form or medium without express written permission of ZD Inc. is prohibited. All other product names and logos are trademarks or registered trademarks of their respective owners. |