Controls that hold ListItem
objects, such as check box lists, radio
button lists, list boxes, and drop-down list boxes, let you add text to each
item using the Value
property (which is of type String
).
That's useful if you want to store more text in each item than is displayed
in the control itself.
For example, in the DropDownListValues example in the code for the book, the
drop-down list displays the abbreviations of various states, CA, MA, NY, and
so on—but behind the scenes, each item in the list has the full text of
each state's name, California, Massachusetts, New York, and so on, stored
in its Value
property. That text is retrieved when the user makes
a selection, as you can see in Figure
14.9.
Figure 14.9
Using the Value
property.
You can set a ListItem
object's Value
property
at runtime in code or at design time using the ListItem Collection editor,
as you see
at right in Figure 14.10.
Figure 14.10
Setting Value
properties.
To work with the current selection in list-oriented controls, you can use
the SelectedIndex
, SelectedItem
, and SelectedValue
properties.
The SelectedValue
property holds the value in the Value
property
of the currently selected item. This means that you can display the full name
of the selected state in the DropDownListValues example like this:
Private Sub DropDownList1_SelectedIndexChanged(ByVal _
sender As System.Object, ByVal e As System.EventArgs) _
Handles DropDownList1.SelectedIndexChanged
TextBox1.Text = "You selected " & DropDownList1.SelectedValue
End Sub
As you can see, the Value
property is useful to store additional
text in each item in a list.
Tip - You might want to store even more information
in list items by deriving a new class from the System.Web.UI.WebControls.ListItem class,
adding a few properties and methods of your own and storing objects of
the new class in a list control. Unfortunately, the ListItem class
is declared NonInheritable , which means that you can't
derive your own classes from it. |
That takes care of list controls for the moment—next, we'll take a look at two other controls that are specifically targeted for Web use: hyperlinks and link buttons.
Comments