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
The CheckBox control represents a checkbox that can have a value of True
or False
or On
or Off
. It has the following HTML syntax:
asp:CheckBox id=CheckBoxName AutoPostBack=True|False
Text=CheckBoxCaption TextAlign=Right|Left
Checked=True|False OnCheckedChanged=OnCheckedChangedMethod
runat=server
The Checked
attribute determines whether the checkbox is initially checked. It corresponds to the CheckBox classs Checked property, which is a Boolean. The
Text attribute defines the caption that
appears to either the left the default or the right of the checkbox itself, depending on the value of the TextAlign
attribute. The Text property of the CheckBox class similarly contains the checkboxs caption, while its alignment is determined by the CheckBox classs TextAlign property. The value of
the TextAlign property is either of the two members of the TextAlign enumeration: TextAlign.Left
, which positions the text to the left of the checkbox the default; and
TextAlign.Right , which
positions the text to the right of the checkbox. The
OnCheckedChanged attribute defines an event handler that will be executed whenever the
Checked property of the checkbox is changed. The event handler designated by the OnCheckedChanged
attribute must have the following signature: Sub OnCheckedChangeMethodsender As Object, e As EventArgs
6.3.1.3 The DropDownList control
The DropDownList control allows the user to make a single selection from a drop-down listbox. The DropDownList control has the following HTML syntax:
asp:DropDownList id=DropDownListName runat=server DataSource= databindingexpression
DataTextField=DataSourceField
256
DataValueField=DataSourceField AutoPostBack=True|False
OnSelectedIndexChanged=OnSelectedIndexChangedMethod asp:ListItem value=value selected=True|False
Text asp:ListItem
asp:DropDownList
Rather than supplying the data either using HTML or programmatically, you can populate the listbox from a data source. The
DataSource attribute defines the data source to which the listbox is bound,
the DataTextField
attribute defines the field that will determine which items are displayed in the listbox, and the
DataValueField attribute defines the field whose rows will provide the value of each
list item. Each of these attributes corresponds directly to a property of the System.Web.UI.WebControls.DropDownList class, which also allows you to dynamically populate a
drop-down listbox at runtime.
In addition, the DropDownList class has a two major properties that dont correspond to attributes of the
DropDownList tag. The SelectedIndex property returns the index of the selected item. The
SelectedItem property returns the selected ListItem object in the drop-down list. Since the DropDownList object allows the user to select only a single item, these properties in most cases allow
you to skip directly working with individual ListItem objects.
If the DropDownList is not bound to a data source, the ListItem
tag is used to define each item in the list.
Text determines the items text thats displayed in the listbox. The
value attribute determines
the items value. The Selected
attribute determines whether or not the item is initially selected when the listbox is displayed.
Finally, the OnSelectedIndexChanged
attribute defines an event handler that will be executed whenever a new item is selected in the drop-down listbox. The event handler designated by the
OnSelectedIndexChanged attribute must have the following signature:
Sub OnSelectedIndexChangedMethodsender As Object, e As EventArgs Programmatically, the items in the drop-down listbox consist of ListItem objects. These can be
accessed through the ListBox class Items property, which returns a ListItemCollection object consisting of all the ListItem objects in the listbox. You can add items to the end of the listbox by
calling the ListItemCollection objects Add method, which has the following syntax:
ListItemCollection.Additem where
item is either a String or a ListItem object. To add an item to a particular place in the drop-
down listbox, you can call the Insert method, which has the following syntax: ListItemCollection.Insertindex, item
where index
is the zero-based position in the listbox at which the item is to be added and item
is a String or a ListItem object to be added to the listbox. You can also remove items from the
ListItemCollection by calling its Remove and RemoveAt methods. The syntax of the Remove method is:
ListItemCollection.Removeitem where
item is a String or a ListItem object representing the item to be removed. The syntax of the
RemoveAt method is:
257
ListItemCollection.RemoveAtindex where
index is the position of the item to be removed from the listbox. The number of items in the
drop-down listbox is provided by the ListItemCollection objects Count property. Three properties of the individual ListItem objects also make it possible to work with the items in the
list. The Text property contains the text of the ListItem object as its displayed in the listbox. The Value property contains the value associated with the ListItem object. Finally, the Selected property returns a
Boolean indicating whether the item is selected.
6.3.1.4 The Image control