Databases Reference
In-Depth Information
Accessing Initial Field Values of Other Columns
The next and biggest challenge is to figure out how to get the current value of the Country
property inside of the Region_Edit field template. The most reliable way to do this is by
getting it from the actual field template instantiated for the Country property.
NOTE
At first thought, the Row property of the FieldTemplateUserControl (the base class
of the custom field template) looks as if it could be used to access other property
values. Logically one would expect this property to return the entity instance, which is
being edited by the field template. This is indeed the case with LINQ to SQL. Entity
Framework, or more specifically the EntityDataSource control, wraps the actual entity
instance inside of an EntityDataSourceWrapper object. The code example that
follows shows how to implement a strongly-typed property that returns the Customer
instance inside of the custom field template:
private Customer Customer
{
get
{
Customer customer = this.Row as Customer;
if (customer == null)
{
var typeDescriptor = (ICustomTypeDescriptor)this.Row;
if (typeDescriptor != null)
customer = (Customer)typeDescriptor.GetPropertyOwner(null);
}
return customer;
}
}
This code works with both LINQ to SQL and Entity Framework, but unfortunately,
there are still two problems with this approach. The Row property of the
FieldTemplateUserControl class is only available during the data binding stage
of the ASP.NET page lifecycle. During post-backs, which is when you would want to
check for a modified Country property value, accessing this property throws an
InvalidOperationException . Moreover, this property returns null when the field
template is instantiated in Insert mode, making it impossible to determine the default
value of the Country property.
The FieldTemplateUserControl base class provides a method called FindOtherFieldTemplate
that takes the name of the entity property as a parameter and returns the field template
that was instantiated for it. The earliest stage in the field template's lifecycle when it
can call this method is the Load . Although it is technically possible to call it during the
Init stage, the result would depend on the order in which the field templates are created,
and the method returns null if the other field template has not been created yet. This
 
Search WWH ::




Custom Search