Databases Reference
In-Depth Information
themselves take care of doing the rest, such as determining the display name of the prop-
erty to use as a label, initializing the mode of the field template based on the mode of the
entity template, and so on. Listing 11.5 shows the updated code-behind. Notice how the
Label_Init and DynamicControl_Init methods shrink to a mere one line of code each,
and the Label_PreRender method, responsible for associating labels with their data
controls, disappears entirely.
LISTING 11.5 Final Version of Default Entity Template (Code-Behind)
using System;
using System.Collections.Generic;
using System.Web.DynamicData;
namespace WebApplication.DynamicData.EntityTemplates
{
public partial class DefaultEntityTemplate : EntityTemplateUserControl
{
private MetaColumn currentColumn;
protected override void OnLoad(EventArgs e)
{
IEnumerable<MetaColumn> scaffoldColumns =
this.Table.GetScaffoldColumns(this.Mode, this.ContainerType);
foreach (MetaColumn column in scaffoldColumns)
{
this.currentColumn = column;
var container = new NamingContainer();
this.entityTemplate.ItemTemplate.InstantiateIn(container);
this.entityTemplate.Controls.Add(container);
}
}
protected void Label_Init(object sender, EventArgs e)
{
((UnleashedLabel)sender).DataField = this.currentColumn.Name;
}
protected void DynamicControl_Init(object sender, EventArgs e)
{
((UnleashedControl)sender).DataField = this.currentColumn.Name;
}
}
}
Search WWH ::




Custom Search