FieldTemplateUserControl Class
Updated: July 2008
Represents the base class for all field template controls.
Assembly: System.Web.DynamicData (in System.Web.DynamicData.dll)
ASP.NET Dynamic Data field templates are ASP.NET user controls that map data controls to data types in a data model. Field templates derive from FieldTemplateUserControl, the class that enables access to data fields, data columns, and metadata in data models. Dynamic Data projects provide field templates by default. You can modify the field templates provided by default or create custom field templates.
This class inherits from the UserControl Class.
The following code example creates a field template that displays data fields that have a many-to-one relationship and renders a HyperLink control that redirects the user to the relationship page.
ForeignKey.ascx.vb file
[Visual Basic]
Public Partial Class ForeignKeyField Inherits System.Web.DynamicData.FieldTemplateUserControl Protected Function GetDisplayString() As String Return FormatFieldValue(ForeignKeyColumn.ParentTable.GetDisplayString(FieldValue)) End Function Public Overloads Overrides ReadOnly Property DataControl() As Control Get Return HyperLink1 End Get End Property End Class
ForeignKey.aspx
[Visual Basic]
<%@ Control Language="VB" CodeFile="ForeignKey.ascx.vb" Inherits="ForeignKeyField" %>
<asp:HyperLink ID="HyperLink1" runat="server"
Text="<%# GetDisplayString() %>" />
[C#]
ForeignKey.ascx.cs
public partial class ForeignKeyField :
System.Web.DynamicData.FieldTemplateUserControl
{
protected string GetDisplayString()
{
Return FormatFieldValue(ForeignKeyColumn.ParentTable.GetDisplayString(FieldValue));
public override Control DataControl
{
get
{
return HyperLink1;
}
}
ForeignKey.aspx
[C#]
<%@ Control Language="C#" CodeFile="ForeignKey.ascx.cs" Inherits="ForeignKeyField" %>
<asp:HyperLink ID="HyperLink1" runat="server"
Text="<%# GetDisplayString() %>" />
To compile the example code, you need the following:
Microsoft Visual Studio 2008 Service Pack 1 or Visual Web Developer 2008 Express Edition Service Pack 1.
The AdventureWorksLT sample database. For informatison about how to download and install the SQL Server sample database, see Microsoft SQL Server Product Samples: Database on the CodePlex site. Make sure that you install the correct version of the sample database for the version of SQL Server that you are running (Microsoft SQL Server 2005 or Microsoft SQL Server 2008).
A Dynamic Data Web site. This enables you to create a data context for the database and the class that contains the data field to customize and the methods to override. In addition, it creates the environment in which to use the page described before. For more information, see Walkthrough: Creating a New Dynamic Data Web Site using Scaffolding.
- AspNetHostingPermission
for operating in a hosted environment. Security action: LinkDemand. Associated enumeration: AspNetHostingPermissionLevel.Minimal
- AspNetHostingPermission
for operating in a hosted environment. Security action: InheritanceDemand. Associated enumeration: AspNetHostingPermissionLevel.Minimal
System.Web.UI.Control
System.Web.UI.TemplateControl
System.Web.UI.UserControl
System.Web.DynamicData.FieldTemplateUserControl
Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.