Databases Reference
In-Depth Information
an open-ended array as its last argument, so if you needed to specify additional control
parameters, you would simply add more names and values to this list.
NOTE
Specifying “WebForms” as the presentation layer in the
UIHintAttribute
constructor
is only required when you need separate UIHint annotations for different presentation
frameworks, such as MVC and WebForms, for the same property. In that case, Dynamic
Data picks the
UIHint
where the presentation layer is specified as “WebForms”.
Otherwise, it uses the
UIHint
for “MVC” or the one where the presentation layer is not
specified. If you are not planning to use
UIHint
annotations with multiple platforms,
leaving it
null
might be the best option.
Unfortunately, the built-in
DynamicControl
does not support custom control parameters
specified in the
UIHintAttribute
the way it supports its own custom attributes specified
in markup. However, you can easily work around this limitation by extending the
dynamic entity templates. As discussed in Chapter 4, “Entity Templates,” and later in
Chapter 10, the dynamic entity templates rely on the
Init
event handler to initialize each
DynamicControl
it creates. Extending this event handler is the simplest way to implement
support for custom parameters. Here is how you can extend the
Init
event handler imple-
mented in
Default.ascx.cs
, the code-behind file of the combined entity template created
in Chapter 10:
protected void DynamicControl_Init(object sender, EventArgs e)
{
var dynamicControl = (DynamicControl)sender;
dynamicControl.DataField = this.currentColumn.Name;
dynamicControl.Mode = this.Mode;
dynamicControl.ValidationGroup = this.ValidationGroup;
var hint = this.currentColumn.Attributes.OfType<UIHintAttribute>()
.FirstOrDefault();
if (hint != null)
{
foreach (KeyValuePair<string, object> p in hint.ControlParameters)
dynamicControl.SetAttribute(p.Key, p.Value.ToString());
}
}
The new code (indicated by bold font) tries to find a
UIHintAttribute
applied to the
current column (a
MetaColumn
object describing an entity property for which a field
template needs to be created). If the attribute was found, it uses the names and values in
the
ControlParameters
dictionary to set custom attributes for the
DynamicControl
. The
DynamicControl
, which has not created the field template yet, takes care of the rest and