JavaScript Editor JavaScript Editor     JavaScript Debugger

Previous Section Next Section

Main Page

Working with Web Form Controls

Developing a Web form-based application is much like developing a Windows form-based application. Visual Basic will manage the files on the server automatically, and you don't have to explicitly upload or download anything, and that's very cool because we can make use of all that Visual Basic already offers us, such as drag-and-drop programming, IntelliSense code prompts, what-you-see-is-what-you-get (WYSIWYG) visual interface designing, project management, and so on. You can fill your Web forms with Web controls, just as you can place Windows controls in a Windows form. But because they run in browsers, Web forms and Web controls are more limited than the Windows variety.

Main Page

Web Server Controls

In fact, there are two varieties of Web form controls—server controls and client controls. Web server controls run not in the browser, but back in the server. That means that when an event occurs, the Web page has to be sent back to the Web server to handle the event. For that reason, Microsoft has restricted Web server control events greatly, mostly handling only Click-type events. And by default, many events—like SelectedIndexChanged events in list boxes—are not sent back to the server at all, but wait until the whole page is sent ("posted" is the Web term) back to the server (which happens when you click a control that is always handled on the server, such as buttons). However, you can force Web server control events like SelectedIndex Changed to be sent back to the server at the time they occur if you set the control's AutoPostBack property to True (see "Forcing Event Handling" in the Immediate Solutions section of this chapter).

Because Web server controls like these are handled back at the server, you can connect Visual Basic code to them. Web server controls often support more functionality than standard HTML controls—but note that they still must run in a browser, so they're actually made up from HTML controls, sometimes in combination with others. You can find the Web server controls—many of which you'll recognize from Windows forms—in Table 14.1. When you want to add these controls to a Web form, you select the Web Forms tab in the toolbox.

Table 14.1: Web server controls.


Does this


A label control.


A text box control.


A control that allows users to select items from a list or enter text directly.


A list box control.


A control that simply displays an image.


A control that displays ad banners.


A checkbox control.


A control that supports a group of checkboxes.


A radio button control.


A control that supports a group of radio buttons.


A control that displays a calendar for choosing dates.


A button control.


A button that looks like a hyperlink but lets you handle Click events like any other button.


A button that displays an image.


A control that displays a hyperlink.


A control that creates an HTML table.


A cell in an HTML table.


A row in an HTML table.


A panel control.


A data control that displays information from a data set using HTML elements.


A control that displays data with more formatting options than a Repeater control.


A control that displays data in a table of columns.

Main Page

HTML Server Controls

Visual Basic creates some Web server controls especially for Web forms, but it also supports the standard HTML controls such as HTML text fields and HTML buttons. You can turn all standard HTML controls into HTML server controls, whose events are handled back at the server. To do that, you right-click a control and select the "Run As Server Control" item. When you do, you can handle such HTML server controls in Visual Basic code in your program by connecting eventhandling code to them just as you would in Windows forms. You can find the HTML server controls in Table 14.2. When you want to add these controls to a Web form, you use the HTML tab in the toolbox.

Table 14.2: HTML server controls.


Does this


Creates an HTML form.


Creates an HTML text field. (You also can use this control to create password fields.)


Creates an HTML text area (two-dimensional text field).


Creates an <a> element for navigation.


Creates an HTML button using the <button> element.


Creates an HTML button using the <input> element.


Creates an HTML button that displays images.


Creates an HTML select control.


Creates an HTML <img> element.


Creates an HTML hidden control.


Creates an HTML checkbox.


Creates an HTML radio button.


Creates an HTML table.


Creates a row in an HTML table.


Creates a cell in an HTML table.


Creates an HTML file upload control.


Creates a basic control for an HTML element.

Main Page

HTML Client Controls

HTML controls don't run on the server by default; they only do so if you rightclick a control and select the Run As Server Control menu item. By default, they are handled in the browser, out of the reach of Visual Basic code. There is an advantage to operating this way; if you handle events in the Web client (the browser) instead of the Web server, the whole page doesn't have to make the round trip to the server, which saves a lot of time. Visual Basic refers to controls handled this way as HTML client controls. Because they run in the browser (as such controls might in any Web page), you have to program them with a language the browser understands, such as JavaScript. You do that with the Visual Basic HTML editor, which allows you to edit the HTML of the Web page directly; we'll see an example of this in this chapter. You add these controls to a Web form just as you add HTML server controls—with the HTML tab in the toolbox—but you don't select the Run As Server Control menu item here.

Main Page

Validation Controls

Besides Web server controls, HTML server controls, and HTML client controls, you also can work with validation controls in Web forms. A validation control lets you test a user's input—for example, you can make sure that the user has entered text into a text field. You also can perform more complex tests, such as comparing what's been entered against a pattern of characters or numbers to make sure things are in the right format. To make these more specific tests, validation controls support regular expressions, which are special expressions used to check text against a pattern to see how well they match that pattern. You can find the validation controls in Table 14.3.

Table 14.3: Validation controls.


Does this


Makes sure the user enters data in this field.


Uses comparison operators to compare user-entered data to a constant value.


Makes sure that user-entered data is in a range between given lower and upper boundaries.


Makes sure that user-entered data matches a regular-expression pattern.


Makes sure user-entered data passes validation criteria that you set yourself.


We'll cover regular expressions in this book, but for the full story, take a look at the Coriolis Perl Black Book, which examines regular expressions in great detail.

Main Page

User Controls

User controls are controls that you create as Web forms pages. You can embed user controls in Web forms, giving you an easy way to create menus, toolbars, and other elements that users are familiar with from Windows forms.

Previous Section Next Section

JavaScript Editor Free JavaScript Editor     JavaScript Editor