JavaScript Editor JavaScript Editor     JavaScript Debugger

Previous Section Next Section

Chapter 15: Web Forms: Buttons, Text Boxes, Labels, Literals, and Place Holders

Main Page

    The Control Class
    The WebControl Class
    Creating Buttons
    Creating Text Boxes
    Creating Labels
    Creating Literals
    Creating Place Holders
    Immediate Solutions: Enabling and Disabling Controls
    Making Controls Visible and Invisible
    Giving Controls Tool Tips
    Setting a Control's Style
    Giving a Control an Access Key
    Moving Controls
    Setting Control Fonts
    Setting Control Border Style
    Using the Button Class
    Creating Buttons
    Creating Command Buttons
    Using the TextBox Class
    Creating Text Boxes
    Creating Multiline Text Boxes
    Creating Password Controls
    Creating Read-only Text Boxes
    Setting Text Box Columns and Rows
    Using the Label Class
    Creating Labels
    Setting Label Text and Style
    Using the Literal Class
    Creating Literals
    Using the PlaceHolder Class
    Adding Controls at Run Time

In Depth

This chapter begins our study of the Visual Basic .NET Web server controls. Web server controls are the ones that have been most closely designed to resemble standard Visual Basic Windows forms controls, and in this and the following chapters, we're going to examine these controls in detail. Web server controls are expressly designed to be handled back on the server, using Visual Basic code.


If you're in doubt about which are the Web server controls, see "Web Server Controls" in the In Depth section of Chapter 14.

It's fortunate that the standard Windows forms controls in Visual Basic so closely resemble the controls you see in browsers already, because that's what makes the whole transition to Web forms possible. Certainly not all Windows form controls can be displayed using the controls native to Web browsers, but many— such as text boxes, list boxes, labels, and others—can. And that's what makes the whole thing work—the fact that Web browsers already support controls that match what Visual Basic offers us in Windows forms in many ways.

We've already seen how to work with Web server controls in a basic way in the previous chapter. All you have to do is to add these controls to a Web form, set their properties in the Properties window, and add server code to them with the appropriate code designer. (Note that you can't add Windows controls to a Web form, because those controls won't work.) To add code to a control's handler, just double-click the control to open its code designer, as with Windows forms.

You also can add Web server controls to a Web form by editing its HTML directly— just switch to HTML view with the HTML button. For example, this code creates a Web server button:

<asp:Button id=Button1 style="Z-INDEX: 101; LEFT: 95px; POSITION:
absolute; TOP: 75px" runat="server" Text="Click me" Width="125px"

Note the syntax here—this element uses the prefix "asp" as a sort of XML-style namespace to indicate that the Web server should handle this element, producing the required HTML. In this case, the server will actually create an HTML Submit button, because when the button is clicked, the entire Web page should be sent back to the server to handle the Click event. Note also the required attribute runat, which is set to "server", indicating that this control's code is to be run back at the server. In addition, note the style attribute, which sets the position mode to "absolute", which means you can set the location of the control yourself (this is how server controls work in grid layout), using the top and left style members to set the location of the upper-left corner of the control, measured in pixels. The text attribute here sets the caption of the button, and the width and height attributes set the dimensions of the button.

It's important to understand the difference between the ID and name of a control. In your server-side Visual Basic code, you refer to the control with the Name property, but in the browser, you refer to the control with its ID attribute's value (which is why the button's ID attribute is set in the above code). In other words, in Visual Basic code on the server, you refer to a control by name; if you're going to add script (such as JavaScript) to it to be run in the browser, you use the control's ID. Usually for server controls, the name and ID are set to the same value.

Web server controls like this button are based on the Control class, but this isn't the Control class we've seen for Windows controls; this is the System.Web.UI.Control class. Because this class is the base class for the controls we'll be working with in the next few chapters, it's important to know the members of the Control class, because they're available in the Web controls derived from this class (which includes the Web form Page class we saw in the previous chapter), and we'll rely on them. For that reason, I'll take a look at the Control class now.

Previous Section Next Section

JavaScript Editor Free JavaScript Editor     JavaScript Editor