- The Anatomy of a FieldTemplate.
- Your First FieldTemplate.
- An Advanced FieldTemplate.
- A Second Advanced FieldTemplate.
- An Advanced FieldTemplate with a GridView.
- An Advanced FieldTemplate with a DetailsView.
- An Advanced FieldTemplate with a GridView/DetailsView Project.
Introduction
After answering a number of questions in the ASP.NET Dynamic Data forum regarding FieldTemplates I thought I write something of an introduction to them.
The Anatomy of a FieldTemplate
All FieldTemplates inherit from FieldTemplateUserControl see below which in turn inherits from UserControl, so a FieldTemplate is really just a UserControl with some extra features added via FieldTemplateUserControl.
public partial class BooleanField : System.Web.DynamicData.FieldTemplateUserControl {
...
}
Figure 1 - DynamicData - FieldTemplateUserControl
The Most Immediately Useful Properties
Here are the main properties and a brief description (My own not Microsoft's :D)
FieldValue (object) – the actual value.
FieldValueString (string) – a string representing the value.
FieldValueEditString (string) - a string representing the value for edit mode.
Row (object) – the whole object in the case of an Order from the Northwind database it contain an object of type Order.
Mode (DataBoundControlMode) – values of Edit, Insert and ReadOnly.
MetaDataAttributes (AttributeCollection) – a collection of attributes for this column.
Column (MetaColumn) – the columns metadata.
Table (MetaTable) – the tables metadata.
The Most Immediately Useful Methods
void PopulateListControl(ListControl listControl) – populates a list control.object GetColumnValue(MetaColumn column) - gets a column values based on the MetaColumn.
The Most Immediately Useful Events
OnDataBinding(EventArgs e) – The DataBinding event is raised when data is bound to the FieldTemplate control. This method notifies the control to perform any data-binding logic that is associated with it. When overriding OnDataBinding be sure to call the base class's OnDataBinding method so that registered delegates receive the event.
protected override void OnDataBinding(EventArgs e) { base.OnDataBinding(e); object val = FieldValue; if (val != null) CheckBox1.Checked = (bool)val; }
Listing 1 – OnDataBinding event from the Boolean FieldTemplate
2 comments:
How does one access the Row object? For example, can I get the Order ID from any FieldTemplate in the case of an Order from the Northwind database?
Each FieldTemplate has a Row property, but you will need to cast it to the entity type you require.
i.e.
Order currentOrder =(Order)Row;
Steve :D
Post a Comment