Freigeben über


Visual Basic Concepts

Making Page Elements Programmable

Elements on a page in a DHTML application are capable of being handled programmatically — that is, you can respond to events for the elements and write code that tells the system what to do when an event is fired. You choose which elements can be programmed by ensuring that those elements for which you want to write code have unique IDs. Only elements with IDs can be programmed.

Note   Some elements are automatically assigned an ID when you add them to a page. For those that are not, you must add the ID yourself by entering a value for the ID property.

An ID is an attribute of an HTML tag that provides a unique identifier for an element on the page. For example, you may have multiple paragraph elements in your document, each marked with the standard <P> HTML tag. Without an ID, there is no way to tell one of these elements from another. Suppose you want to perform an operation on a single paragraph in your page, such as turning all of the text in that paragraph blue, but you do not want to apply that change to any other paragraphs in the page. Without an ID attribute, you cannot specify that the operation should affect any one <P>, or paragraph. However, if you assign a unique ID to the paragraph you want to change, you can reference just that paragraph in your code.

For Visual Basic, all of the IDs on a page must be unique. This is not true of HTML pages outside of Visual Basic, so it is possible that you may import a page that contains duplicate IDs. Visual Basic will automatically append numbers to the end of any nonunique IDs during the import process.

After you add an ID to an element, it appears in bold in the treeview. This shows you at a glance the elements that are currently programmable in your page. You can double-click on any programmable element in the treeview or detail panel to move to the Code Editor window for that element.

Note   You cannot write code in a form, message box, or other object that references HTML elements on the page. Page elements are only accessible within the page itself.

Name vs. ID

In forms-based Visual Basic applications, the Name property acts as the main identifying property for controls on the page. For HTML elements, the ID property is the value that uniquely identifies the element in code. In most cases, the Name property does not play an important role in the life of elements on an HTML page.

There is one situation in which the Name property is useful for HTML page elements: making a set of option buttons mutually exclusive. For example, suppose you have three option buttons that are intended to identify how the user is going to pay for an order: with cash, with a credit card, or with a check. In a forms-based application, you would make these elements part of a control array, thus allowing the user to select only one of them. In a DHTML application, you use the Name property to produce the same result. For each option button you want to group together, you set the Name property to the exact same value. For example, you might set the Name property for these three options to "PaymentOptions." This makes the option buttons function as a group — when one is selected, the other two are automatically deselected.

Note   Even though you assign the same Name property to the three option buttons in this example, they must still have unique ID values. The value of the ID property must always be unique in Visual Basic.

To make an element programmable

  1. In either the treeview or the detail panel, click the element for which you want to assign an ID.

  2. In the Properties window, enter a value for the ID property, then press ENTER.

    The system adds the element to the Object list in the Code Editor window, and makes the name of the element bold in the treeview.

To access the Code Editor window for a programmable element

  • Double-click on the element in the designer's treeview or detail panel.