Databases Reference
In-Depth Information
LISTING 10.16
Continued
TypeDescriptor.GetConverter(this.Column.ColumnType);
return converter.ConvertFrom(dictionary[this.Column.Name]);
}
}
}
FieldValue Property
The built-in
FieldTemplateUserControl
base class already provides a property called
FieldValue
. However, it is available only during the data binding stage of the page life-
cycle, and it throws an
InvalidOperationException
if you try accessing it during post-
back. This limitation also affects the
FieldValueString
and
FieldValueEditString
properties that return a formatted version of the
FieldValue
. Luckily, the
FieldValue
property is declared as virtual, and the
UnleashedFieldTemplate
overrides it to remove
this limitation.
The overridden
FieldValue
property getter calls the inherited getter, which invokes the
GetColumnValue
method to do the actual work. This method is overridden in the
UnleashedFieldTemplate
class to catch the
InvalidOpertationException
and call the
ExtractFieldValue
method, which is discussed shortly, to retrieve the current field value
from the template's data control. The overridden setter of the
FieldValue
property checks
if the new field value is different from the old one and calls the
OnFieldValueChanged
method to raise the
FieldValueChanged
event if that is the case.
ExtractFieldValue Method
The
ExtractFieldValue
method calls the
ExtractValues
method, which is defined as
virtual in the
FieldTemplateUserControl
class specifically for extracting the current field
value from the template's data control. The
ExtractValues
method is already imple-
mented in all appropriate Edit mode field templates that come with Dynamic Data
projects, and you can simply call it here instead of reinventing the wheel. It takes an
OrderedDictionary
as a parameter and populates it with column names and values.
Because a particular data control might store a field value in a different format, such as a
TextBox
storing a numeric value as a string, the
ExtractFieldValue
method uses a
TypeConverter
to convert the extracted field value to the actual type of the column.
The
ExtractFieldValue
method has a special logic for handling foreign key columns. At
the time of data binding, a foreign key column is a navigation property, so the
FieldValue
property returns an actual target entity instance, such as a
Customer
instance. At post-back
time, however, there are no entity instances, and you can only get the values of the
underlying primitive columns, such as a CustomerID. If the foreign key consists of multi-
ple primitive columns, the
ExtractFieldValue
method returns a coma-separated list of
their values, which is also how the built-in
ForeignKey_Edit
field template stores them in
the
ListItem
instances of its
DropDownList
control.