The Button control Web Controls

254 Instances of web controls declared using HTML syntax have a number of common attributes that are worth noting. The ID attribute defines the name of the instance of the web control and should be used in all statements that create web controls. It corresponds to the control classs ID property, which defines the name by which the control is referenced programmatically. A second is AutoPostBack , an optional attribute common to many controls, which determines whether the state of the control is posted back to the server whenever it is clicked. By default, its value is False . It corresponds directly to the control classs AutoPostBack property. Finally, the RunAt attribute is required when creating a web control; without it, the downstream browser will attempt to interpret the HTML tag and will simply fail to render the control. Each web control also has a class definition in the System.Web.UI.WebControls namespace of the .NET Framework Class Library. By using its ID attribute or ID property, you can reference in code a web control created using HTML syntax. For example, the following statement unchecks the CheckBox control we defined in the previous code fragment: chkMail.Checked = False Web controls can also be created programmatically, as the following code fragment, which creates a CheckBox control identical to the one defined earlier using HTML syntax, shows: Dim chkMail As New CheckBox chkMail.Text= Be placed on our mailing list? chkMail.Checked = True chkMail.AutoPostBack = False Note that, except for RunAt , the attributes available for a control using HTML syntax correspond directly to a property of the control class both in name and in data type. The following sections document the web controls available in the .NET Framework.

6.3.1.1 The Button control

The Button server control represents a command button and can be used as a Submit button its default behavior or any other type of command button on a form. The Submit button has the following HTML syntax: asp:Button id=ButtonName Text=label OnClick=OnClickMethod runat=server When used as a command button for some other purpose, the Button control has the following HTML syntax: asp:Button id=ButtonName Text=label CommandName=command CommandArgument=commandArgument OnCommand=OnCommandMethod runat=server The OnClick attribute defines the event handler that is executed when the button is clicked. The Text attribute determines the caption displayed on the button face. If used as a Submit button, the CommandName and CommandArgument attributes are not used; otherwise, at a minimum, the CommandName attribute, which indicates the command the button is to carry out, should be present. The optional CommandArguments attribute provides any arguments needed to execute the command. 255 The Text , CommandName , and CommandArgument attributes all correspond to identically named properties of the System.Web.UI.WebControls.Button class. When the Submit button is clicked, the event handler defined by the OnClick attribute is executed. The signature of the event is: Sub OnClickMethodsender As Object, e As EventArgs For any other command button, the Command event corresponds to the Submit buttons Click event, and the event handler defined by the OnCommand attribute is executed. The event handler must have the following signature: Sub OnCommandMethodsender As Object, e As CommandEventArgs The CommandEventArgs object has two properties: CommandName, which corresponds to the CommandName attribute and represents the name of the command; and CommandArgument, which corresponds to the CommandArgument attribute and represents an argument supplied to the command. These property values can be used to identify which button was clicked in the event that a single event handler is used for more than one Button control.

6.3.1.2 The CheckBox control