How To: Create Custom Dynamic Data Fields
Introduction
This topic demonstrates how to create custom dynamic data fields. ASP.NET Dynamic Data generates a set of dynamic data fields that are derived from the FieldTemplateUserControlBase base type. These dynamic data fields are user controls that are used to render data fields.
You can create custom dynamic data fields for your own rendering or validation needs. ASP.NET Dynamic Data determines the custom control to use for rendering based on the RenderHintAttribute attribute that is applied to a class that is part of the custom dynamic data field.
The following example uses a custom UnitsInStockControl.ascx user control. When you run the example, the application displays the UnitsInStock data field using yellow background and red foreground if the particular product is understocked.
To run the example code in this topic, you will need the following:
Creating a Custom Dynamic Field
To customize data rendering, you start by creating user control (.ascx file) that inherits from FieldTemplateUserControlBase.
To create a custom dynamic data field
-
In the App_Shared/DynamicDataFields folder, create a new .ascx file, such as UnitsInStockControl.ascx.
-
In the @ Control directive, add an Inherits attribute that references the System.Web.DynamicData.FieldTemplateUserControlBase class, as shown in the following example:
<%@ Control Language="C#"
Inherits="System.Web.DynamicData.FieldTemplateUserControlBase" %>
-
Create the markup that will be rendered to display the data.
The following example shows a Label control whose Text property is set to the current data value string.
<asp:Label id="CurrentValueID" runat="server"
Text='<%# DataValueString %>'>
</asp:Label>
-
To customize how the control renders data, handle the DataBinding event of the user control (or controls). In the handler, you can get the value of the current data field from the control's DataValue property.
The following example shows a complete .ascx file that includes markup and code to handle the DataBinding event.
CS
<%@ Control Language="C#"
Inherits="System.Web.DynamicData.FieldTemplateUserControlBase" %>
<script runat="server">
// DataBinding event handler.
public void BindData(object sender, EventArgs e)
{
short threshold = 100;
// Get the current bound value.
short currentValue = (short)DataValue;
// Check if the number of items is understocked.
if (currentValue < threshold)
{
CurrentValueID.BackColor = System.Drawing.Color.Yellow;
CurrentValueID.ForeColor = System.Drawing.Color.Red;
}
}
</script>
<asp:Label id="CurrentValueID" OnDataBinding="BindData"
Text='<%# DataValueString %>' runat="server"></asp:Label>
VB
<%@ Control Language="VB"
Inherits="System.Web.DynamicData.FieldTemplateUserControlBase" %>
<script runat="server">
' DataBinding event handler.
Public Sub BindData(ByVal sender As Object, ByVal e As EventArgs)
Dim threshold As Short = 100
' Get the current bound value.
Dim currentValue As Short = CType(DataValue, Short)
' Check if the number of items is understocked.
If currentValue < threshold Then
CurrentValueID.BackColor = System.Drawing.Color.Yellow
CurrentValueID.ForeColor = System.Drawing.Color.Red
End If
End Sub
</script>
<asp:Label id="CurrentValueID" OnDataBinding="BindData"
Text='<%# DataValueString %>' runat="server"></asp:Label>
The code checks the stock level of products. If a product is understocked, the control displays the units using red foreground and yellow background.
Next you must associate the table column that you want to display with the custom control.
To associate a table column with a custom user control
-
In the App_Code folder, create a new class file that has the name of the database table that contains the data for the custom control to display.
For example, if the custom control will display data from the Products table, name the class Products.
-
Add the partial keyword (Partial in Visual Basic) to the class definition. The class you are creating is a partial class that is part of the database model.
-
Add the RenderHintAttribute attribute to the partial class definition. The attribute's first parameter is the name of the field (column) to which the rendering will be applied. The second parameter specifies the dynamic data field to use.
The following example shows a partial class definition that renders the UnitsInStock dynamic field by using the UnitsInStockControl.ascx control.
[RenderHint("UnitsInStock", "UnitsInStockControl")]
public partial class Product {
}
This topic is ASP.NET Extensions documentation and is unsupported by Microsoft. Blank topics are included as placeholders and existing content is subject to change in future releases.