Databases Reference
In-Depth Information
}
protected override void ExtractValues(IOrderedDictionary dictionary)
{
this.ExtractForeignKey(dictionary, this.dropDownList.SelectedValue);
}
}
}
To display the current field value, the
ForeignKey_Edit
template cannot rely on the
FieldValueEditString
property because the
FieldValue
in this case consists of one or
more values of the underlying primitive columns. For the Customer column of the Order
table, this would mean displaying CustomerID, “ALFKI”, instead of the Customer's
CompanyName, “Alfreds Futterkiste”, which is not very user friendly. Instead, the
ForeignKey_Edit
template overrides the
OnDataBinding
method to select the matching
item in the
DropDownList
. It calls the
GetSelectedValueString
, a convenience method
provided by the
FieldTemplateUserControl
base class. For foreign key columns, this
method is equivalent to calling
ForeignKeyColumn.GetForeignKeyString(Row)
and returns
a comma-separated list of values of the primitive columns in the foreign key.
The
ForeignKey_Edit
template also overrides the
ExtractValues
method and calls the
ExtractForeignKey
provided by the
FieldTemplateUserControl
base class. Given a
comma-separated list of column values, this method stores them in the dictionary under
the names of the foreign key columns. Under the hood, this method simply calls
ForeignKeyColumn.ExtractForeignKey(dictionary, selectedValue)
.
The set of field templates provided by Dynamic Data can be extended whenever a special
data entry control is needed to handle values of a certain type. For example, the
Northwind database uses regular string columns to store phone numbers for customers,
employees, suppliers, and shippers. The phone numbers already stored in the database are
in a consistent
(###)###-####
format; however, Dynamic Data uses the regular
Text_Edit
field template for these columns, which does not enforce the phone number format. It
would be nice to create a field template that would help users enter the phone numbers
correctly.
You create a new field template by adding a new user control to the
DynamicData\FieldTemplates
folder of your project. You can also copy an existing field
template that most closely matches the desired functionality. If you do copy an existing
field template, remember to change the class name in both markup and code-behind files.
In this example, however, we will start from scratch and create a new user control called
PhoneNumber_Edit
, shown in Listing 3.11.