JavaScript Editor JavaScript Editor     JavaScript Debugger

Previous Section Next Section

Main Page

Creating Buttons

You use the Button control to create a button in a Web page. Here is the class hierarchy of this control; note that it's based on the WebControl class, which in turn is based on the Control class:

Object
   Control
      WebControl
         Button

Web server button controls post their data back to the server when they're clicked, so by default, they're made into Submit buttons in HTML by the Web server. (Submit buttons are the ones you click expressly to send data to the server.) Here's what a typical button looks like in HTML:

<input type="submit" name="Button1" value="Click me" id="Button1"
style="height:24px;width:125px;Z-INDEX: 101; LEFT: 95px; POSITION:
absolute; TOP: 75px" />

That's the HTML representation of the button, but in code, you can stick to Visual Basic; in fact, the Click event handler for buttons looks exactly as it would in a Windows forms project, with the same two arguments passed to this Sub procedure:

    Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click
        TextBox1.Text = "You clicked the button."
    End Sub

You also can add a command name to a button to create a command button. (VB .NET makes a distinction between simple buttons, which are Submit buttons, and command buttons.) You can add a command name to a button with the CommandName property, and a command argument with the Command Argument properties. Both of these properties can hold text; you can recover that text in your Visual Basic code. This is useful, for example, if you want to use only one button event handler for all the buttons in your Web page (which you can do if you embed your Visual Basic in the Web page—see "Embedding Visual Basic Code in Web Pages" in Chapter 14 for more details). To determine which button was clicked, you can just check the CommandName property of the button that caused the event.

The big event for buttons, of course, is the Click event; we'll put that event to work in this chapter. The other main event is the Command event, which you add code to if you're working with command buttons that have a command name. For our purposes, working with Web server buttons will be much like working with Windows buttons, which gives you an indication of how well Microsoft has been able to port the functionality of button controls to Web forms.

Previous Section Next Section




JavaScript Editor Free JavaScript Editor     JavaScript Editor