Databases Reference
In-Depth Information
LISTING 1.12 Default_Edit Entity Template (Code-Behind)
using System;
using System.Web.DynamicData;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication.DynamicData.EntityTemplates
{
public partial class Default_EditEntityTemplate : 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;
Control namingContainer = new DefaultEntityTemplate._NamingContainer();
this.entityTemplate.ItemTemplate.InstantiateIn(namingContainer);
this.entityTemplate.Controls.Add(namingContainer);
}
}
protected void Label_Init(object sender, EventArgs e)
{
((Label)sender).Text = this.currentColumn.DisplayName;
}
protected void DynamicControl_Init(object sender, EventArgs e)
{
((DynamicControl)sender).DataField = this.currentColumn.Name;
}
}
}
As you can see, the control overrides the OnLoad method, retrieves the list of metadata
columns from the table and instantiates the EntityTemplate inside the foreach loop for
every column. The Label_Init event handler initializes label's text with the display name
of the column and the DynamicControl_Init event handler initializes the dynamic control
with the name of the column itself. The rest is done by the DynamicControl instances,
which load and render the appropriate field templates based on the column metadata.
 
Search WWH ::




Custom Search