Visual Basic Concepts

Adding HTML Templates to the Webclass

You add HTML templates to your IIS application to enable your webclass to send HTML pages to the browser in response to user requests.  When you add a template to your webclass, you choose an HTML page you want to associate with it. Visual Basic then scans that file and looks for HTML tags that are capable of launching a request to the server. Such tags include form elements, image tags, hyperlinks, and most other tags that contain an reference.

Note   Although a webclass can contain multiple template webitems, each template webitem can represent only one HTML page. You must add additional templates for each HTML page you want to include in your application.

Tags in the HTML page can contain attributes that launch a server request. For example, in an IMG tag, the SRC attribute is used to specify a URL location from which to retrieve a file. This attribute, and others like it that call the server, can become events in your IIS application. Visual Basic lists each of these tags and attributes in the right panel of the designer window. The name of each tag is taken from the ID attribute for that tag, if one exists. An ID attribute is an optional HTML attribute that you add to the .htm file itself. For each HTML tag in the file, you can assign a unique ID that can be used to identify that tag in script or code.

Note   You can use Visual Basic to assign the name of the webclass or its templates, but you cannot use the Properties window to assign an ID to the selected tag.

If your HTML template file contains a tag that does not have an ID, the designer assigns it an ID based on its position in the HTML page. For example, the third <A> tag without an ID would be named Hyperlink3. This ID is not permanent; if you do not connect any of the tag attributes to events or webitems, the ID is not stored in the HTML page. However, when you connect one of the tag's attributes, the ID becomes permanent and is stored in the .dsr file for the page.

You must save your project before you can add a template to it. After you add the template, Visual Basic does one of the following things:

  • If the .htm file you chose for the template was located outside the project directory, Visual Basic makes a copy of the template file and places it in the project directory.

  • If the .htm file you chose for the template came from the project directory or if there is another .htm file there with the same name, the system creates a copy of the .htm file but appends a number to its name. For example, a file called OrderForm.htm would be changed to OrderForm1.htm.

The action of copying the .htm file occurs when you save or debug the project, or when you use the Edit HTML Template menu command.

Important   From this point on, the .htm file in the project directory acts as the source file for your project. If you want to make changes to the HTML page's appearance, you do so in this copy of the HTML page.

If your HTML template file references any additional files, such as images, you must copy those files into the project directory or a subdirectory of it. You can do this before you add the template to the webclass or after, but you must do it before debugging or running the project.

Note   If you add a template to your webclass that contains badly formatted HTML, you may see errors in the webclass designer when it loads the template. To prevent this, it is a good idea to run your HTML files through an HTML syntax checker before loading them into the designer.

In addition, if you import an HTML page for which a similarly named .htm file already exists in the project directory, VB appends a number to the .htm file it creates for the new file. This prevents Visual Basic from overwriting an existing .htm file if you reuse a template in more than one webclass. For example, suppose you have an HTML page called feedbk.htm. The first time you add this to a webclass, Visual Basic generates a file in the project directory with the name feedbk.htm. If you add this same file to another webclass in the application, the new .htm file Visual Basic creates is named feedbk1.htm.

For More Information   For more information on the directory structure you should use for your projects, see "Managing your Project Files." For more information on tags and attributes, see "Understanding Internet Basics" in "Introduction to Internet Applications."

To add an HTML template to an IIS application

  1. In the Webclass Designer window, click the Add HTML Template WebItem button on the Webclass toolbar.

  2. In the Add HTML Template dialog box, select the HTML page you want, then click OK.

  3. If the template file you chose references any additional files, such as .gif or .jpg files, copy those files to the project directory or a subdirectory beneath it.

    Note   If the template file contains absolute references to the previous locations of those files on the development computer, you need to make changes to the .htm file to reflect the location the files will have when deployed to the server. See "Managing Your Project Files" for more information on using relative and absolute paths in your template files.

Removing a Template from a Webclass

You can remove a template from a webclass if you no longer want to send that template file to the browser in response to requests. You cannot replace one template with another; instead you must remove the first template file and add a link to a new file. If the template files have the same name, any event procedures you wrote for the former template will be available to the new template.

For example, suppose you are working with a catalog application and want to change the order form your application uses. You delete a template file called OrderForm, then add another HTML page with the same name that contains a different order entry form. All event procedures associated with the original OrderForm file, such as OrderForm_Respond, are immediately associated with the new file.

To remove a template from the webclass

  • Right-click the template webitem and click Delete.

Editing the HTML for a Template

After you have inserted an HTML template file, you can use the HTML editor of your choice to make changes to the page's content and layout. You do so by selecting the Edit HTML Template option from the context menu for a template. Visual Basic opens the .htm file from the project directory. After you finish editing, the designer displays a prompt asking if you want to refresh the file. You must refresh the file if you want to see the changes you made.

Note   You will not receive this prompt if the designer did not have focus before you edited the template. In this case, you must manually refresh the file by using the template's context menu.

By default, Visual Basic displays either your default HTML editor or Notepad when you edit your template. Visual Basic automatically detects your default HTML editor by looking at the system registry. You can determine the program that opens when you edit your template by making an entry in the External HTML Editor field found on the Advanced tab of the Project Options dialog box.

Tip   If you want to add buttons to your HTML template or program buttons that already exist on the page, you must make sure that the HTML for the buttons contains the attribute type=SUBMIT.

To edit your HTML webitem

  1. In the left panel of the Designer window, right-click the template for which you want to edit the HTML, then select Edit HTML Template.

  2. Make your changes to the .htm file, save, and return to Visual Basic.

  3. If you have made any changes to the event tags in the file, right-click the webitem and select Refresh HTML Template, or respond to the prompt to refresh.

    Visual Basic parses the file.

Saving Changes to Your Template

If you want to update the webclass with changes you have made to the .htm file, you refresh the link between your template and its external file. When you refresh the template, Visual Basic checks to see whether the events currently listed for the template still exist, and whether any new attributes should be added. The program adds any new attributes and processes any changes for existing events. If an event for the template no longer exists in the .htm file, Visual Basic removes it from the webclass. Any procedures you wrote for the event are not removed — you must clean up the event procedures yourself.

If another user makes changes to an .htm file used in your webclass, you receive a prompt asking whether you want to refresh the template. This message appears each time a user saves the .htm file in an editor.

To save changes to the .htm file you've referenced

  • In the left panel of the designer window, right-click the template for which you want to refresh your import, then select Refresh HTML Template.

    Visual Basic parses the file.

For More Information   See "Connecting Events in a Webclass" for more information on connecting and disconnecting events for a webitem.