When used in a data binding situation, the label is often used
to represent read-only data. When the data bound control is rendered in edit
mode, a text box is used to edit the data. However, sometimes a list is used,
which performs two bindings (depending on the situation). The first binding is
the binding of all the possible values in the list. The second binding is which
value was selected in the list. However, in view mode, this means some
challenges; for instance, oftentimes the value stored in the database is an
encoded value (like O for Open and C for close). This means that the value has
to be converted, which may not be a challenge.
Let's use for example a United States state code field. In
the database, it stores PA for Pennsylvania, NY for New York, etc. In edit
mode, a drop down list is used, showing Pennsylvania and New York values
spelled out; however, the underlying value is the encoded two-letter form. In
read-only mode, whenever the main data source is bound, it only knows about the
two-letter form.
There are multiple solutions to this. The first is to
translate the value. The GridView control has a RowDataBound method that can be
used to tap into the rows/cells, and change the value stored in the cell (if
that is the control that is being used). Another alternative is to include both
the code and the name in the underlying data source. But with a little
customization, another alternative is possible; that is what we will look at
next.