Monday, 21 July 2008

Dynamic Data and Field Templates - The Anatomy of a FieldTemplate

  1. The Anatomy of a FieldTemplate.
  2. Your First FieldTemplate.
  3. An Advanced FieldTemplate.
  4. A Second Advanced FieldTemplate.
  5. An Advanced FieldTemplate with a GridView.
  6. An Advanced FieldTemplate with a DetailsView.
  7. 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
{
...


}

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

Note: It is only during and after this event OnDataBinding the FiledValue, FieldValueString and FieldValueEditString are valid. So you can’t access them in the Page_Init or Page_Load events.

2 comments:

Anonymous said...

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?

Stephen J. Naughton said...

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