![]() ![]() | ||
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.
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.
Control |
Does this |
---|---|
Label |
A label control. |
TextBox |
A text box control. |
DropDownList |
A control that allows users to select items from a list or enter text directly. |
ListBox |
A list box control. |
Image |
A control that simply displays an image. |
AdRotator |
A control that displays ad banners. |
CheckBox |
A checkbox control. |
CheckBoxList |
A control that supports a group of checkboxes. |
RadioButton |
A radio button control. |
RadioButtonList |
A control that supports a group of radio buttons. |
Calendar |
A control that displays a calendar for choosing dates. |
Button |
A button control. |
LinkButton |
A button that looks like a hyperlink but lets you handle Click events like any other button. |
ImageButton |
A button that displays an image. |
HyperLink |
A control that displays a hyperlink. |
Table |
A control that creates an HTML table. |
TableCell |
A cell in an HTML table. |
TableRow |
A row in an HTML table. |
Panel |
A panel control. |
Repeater |
A data control that displays information from a data set using HTML elements. |
DataList |
A control that displays data with more formatting options than a Repeater control. |
DataGrid |
A control that displays data in a table of columns. |
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.
Control |
Does this |
---|---|
HtmlForm |
Creates an HTML form. |
HtmlInputText |
Creates an HTML text field. (You also can use this control to create password fields.) |
HtmlTextArea |
Creates an HTML text area (two-dimensional text field). |
HtmlAnchor |
Creates an <a> element for navigation. |
HtmlButton |
Creates an HTML button using the <button> element. |
HtmlInputButton |
Creates an HTML button using the <input> element. |
HtmlInputImage |
Creates an HTML button that displays images. |
HtmlSelect |
Creates an HTML select control. |
HtmlImage |
Creates an HTML <img> element. |
HtmlInputHidden |
Creates an HTML hidden control. |
HtmlInputCheckbox |
Creates an HTML checkbox. |
HtmlInputRadioButton |
Creates an HTML radio button. |
HtmlTable |
Creates an HTML table. |
HtmlTableRow |
Creates a row in an HTML table. |
HtmlTableCell |
Creates a cell in an HTML table. |
HtmlInputFile |
Creates an HTML file upload control. |
HtmlGenericControl |
Creates a basic control for an HTML element. |
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.
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.
Control |
Does this |
---|---|
RequiredFieldValidator |
Makes sure the user enters data in this field. |
CompareValidator |
Uses comparison operators to compare user-entered data to a constant value. |
RangeValidator |
Makes sure that user-entered data is in a range between given lower and upper boundaries. |
RegularExpressionValidator |
Makes sure that user-entered data matches a regular-expression pattern. |
CustomValidator |
Makes sure user-entered data passes validation criteria that you set yourself. |
Tip |
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. |
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.
![]() ![]() | ||