Databases Reference
In-Depth Information
LISTING 10.8
Continued
base.OnPreRender(e);
var fieldTemplate = this.
FindFieldTemplate
(this.DataField)
as FieldTemplateUserControl;
if (fieldTemplate != null && fieldTemplate.DataControl != null)
{
this.AssociatedControlID =
fieldTemplate.DataControl.GetUniqueIDRelativeTo(this);
}
}
}
}
Most of the work in
UnleashedLabel
is done by the
OnInit
method. It uses the
FindMetaTable
extension method defined in the
DynamicDataExtensions
class to find the
MetaTable
object associated with the
FormView
or
GridView
hosting this control. As you
recall from Chapter 7, “Metadata API,” all page templates in Dynamic Data have to associ-
ate a
MetaTable
object with their databound controls either with the help of a
DynamicDataManager
control or explicitly in code-behind. The
UnleashedLabel
control
takes advantage of this fact similar to how the
DynamicControl
does.
Having obtained the
MetaTable
object, the
OnInit
method calls its
GetColumn
method to
find the
MetaColumn
object describing the entity property based on the
DataField
property
supplied in markup. After the
MetaColumn
object has been retrieved, its
DisplayName
prop-
erty value is then used to initialize the
Text
property of the label.
Aside from having to find the
MetaColumn
object programmatically, the
OnInit
method in
UnleashedLabel
performs the same function as the
Label_Init
event handler in the
dynamic entity template shown in Listing 10.6; it initializes the label text based on the
display name of the entity property. Likewise, the
OnPreRender
method is very similar to
the
Label_PreRender
event handler in that it associates the label with the data control
generated by the field template. The only significant difference here is that this code relies
on the
FindFieldTemplate
extension method defined in the
DynamicDataExtensions
class
to find the field template instead of accessing the corresponding
DynamicControl
directly.
NOTE
To use this control in mar kup, you could register it in the various pages and user
controls individually. However, because the sample solution uses it pervasively, it
makes sense to register it globally by adding the following line to the
System.Web/Pages/Controls section of the Web.config file:
<add tagPrefix=”unl”
assembly=”Unleashed.DynamicData”
namespace=”Unleashed.DynamicData “/>