The ListBox control Web Controls

258 ImageAlign.Right The image is aligned on the right edge of the web page, with text wrapping on the left. ImageAlign.TextTop The images top edge is aligned with the top edge of the highest text on the line. ImageAlign.Top The images top edge is aligned with the top edge of the highest element on the same line.

6.3.1.5 The Label control

The Label control typically displays static text. Perhaps the simplest of controls, it has the following HTML syntax: asp:Label id=Label1 Text=label runat=server The text displayed by the label is represented by the Text attribute. It directly corresponds to the identically named property of the System.Web.UI.WebControls.Label control.

6.3.1.6 The ListBox control

The ListBox control presents the user with a list of items and allows the user to select either a single item or multiple items. The ListBox control has the following HTML syntax: asp:ListBox id=ListboxName DataSource= databindingexpression DataTextField=DataSourceField DataValueField=DataSourceField AutoPostBack=True|False Rows=rowcount SelectionMode=Single|Multiple OnSelectedIndexChanged=OnSelectedIndexChangedMethod runat=server asp:ListItem value=value selected=True|False Text asp:ListItem asp:ListBox The SelectionMode attribute determines whether the user can select only a single item the default or multiple items. The Rows attribute determines the number of rows displayed in the listbox. Rather than supplying the data either using HTML or programmatically, you can populate the list box 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 SelectionMode , Rows , DataSource , DataTextField , DataValueField directly corresponds to a property of the System.Web.UI.WebControls.ListBox 259 class. The value of the SelectionMode property must be a member of the ListSelectionMode enumeration; its two possible values are ListSelectionMode.Single and ListSelectionMode.Multiple . The Rows property is an Integer that determines how many rows the listbox displays. In addition, the ListBox class has a two major properties that dont correspond to attributes of the ListBox tag. The SelectedIndex property returns the lowest index of a selected item. The SelectedItem property returns the first selected ListItem object in the listbox. For single-selection listboxes, these properties return the index of the selected item and the selected item itself, respectively. For multi-selection listboxes, you have to iterate the ListItemCollection collection to determine which items are selected. If the listbox 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 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 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 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: ListItemCollection.RemoveAtindex where index is the position of the item to be removed from the listbox. The number of items in the 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. In the case of multi-selection listboxes, you can determine whether an item is selected by iterating the members of the ListItemCollection object and examining the Selected property of each, as the following code fragment shows: 260 Dim oList As ListItem For Each oList In oListBox.Items If oList.Selected Then Do something End If Next

6.3.1.7 The RadioButton control