Export (0) Print
Expand All

DynamicControl Class

Updated: July 2008

Displays the content that is defined for the field in templated data-bound controls, using ASP.NET Dynamic Data features.

Namespace:  System.Web.DynamicData
Assembly:  System.Web.DynamicData (in System.Web.DynamicData.dll)

[ToolboxBitmapAttribute(typeof(DynamicControl), "DynamicControl.ico")]
[AspNetHostingPermissionAttribute(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermissionAttribute(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
public class DynamicControl : Control, 
	IAttributeAccessor, IFieldTemplateHost, IFieldFormattingOptions
<asp:DynamicControl />

The DynamicControl control is used by templated data-bound controls, such as FormView or ListView, to display a data field that uses ASP.NET Dynamic Data features in a custom page. You can also use a DynamicControl control in a TemplateField field of a GridView or a DetailsView control.

The DynamicControl control provides functionality that is similar to that of the DynamicField field. However, because the DynamicControl control is only used inside templates, it gives you more flexibility in controlling how the data is displayed since you can add other HTML elements around the field. When you use a DynamicControl control, you can take advantage of the following Dynamic Data features:

•Automatically rendering the proper control for a field by using field templates, based on the data type. The field templates can be globally modified for the entire application.

•Providing built-in data validation based on the database schema. You can also add validation types by customizing the data model.

•Customizing data rendering for individual fields by using attributes in the data model or by using the P:System.Web.DynamicData.DynamicField.UIHint property.

To specify the field to display in a DynamicControl control, set the DataField property to the field name. You can apply a custom formatting string to the field value by setting the DataFormatString property. By default, the formatting string is applied to field values only when the data-bound control is in read-only mode. To apply the formatting string to values displayed while the data-bound control is in edit mode, set the ApplyFormatInEditMode property to true. If a field value is null, you can display a custom caption by setting the NullDisplayText property. The DynamicControl control can also automatically convert empty string ("") field values to null values by setting the ConvertEmptyStringToNull property to true.

The DynamicControl control supports different display modes. To specify the mode in which to display the DynamicControl control, set the Mode property. The following table shows the different modes.

ReadOnly

for display mode.This prevents the user from modifying the values of a record or a data field. It can be used in any of the templates.

Edit

for edit mode.This enables users to update the values of an existing record. It is used in the edit template (typically called EditItemTemplate).

Insert

for insert mode. This enables users to enter values for a new record. It is used in the insert template (typically called InsertItemTemplate).

You can hide a DynamicControl control in a data-bound control by setting the Visible property to false.

See a run-time code example of this feature: Run.

The following example demonstrates how to use the DynamicControl control in a ListView control to display, insert, delete and edit values from a database table. It is important to notice that in order to make the insert operation to work, the IsDbGenerated property of the rowguid and ModifiedDate columns are set to true.

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
  protected void Page_Init(object sender, EventArgs e)
  {
    DynamicDataManager1.RegisterControl(ListView1);
  }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title>DynamicControl Sample</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <asp:DynamicDataManager ID="DynamicDataManager1" runat="server"
        AutoLoadForeignKeys="true" />

      <asp:ValidationSummary ID="InsertValidationSummary" runat="server" EnableClientScript="true" 
        HeaderText="List of validation errors" ValidationGroup="Insert" />        
      <asp:DynamicValidator runat="server" ID="InsertValidator"
        ControlToValidate="ListView1" ValidationGroup="Insert" Display="None" />
      <asp:ValidationSummary ID="EditValidationSummary" runat="server" EnableClientScript="true" 
        HeaderText="List of validation errors" ValidationGroup="Edit" />
      <asp:DynamicValidator runat="server" ID="EditValidator" 
        ControlToValidate="ListView1" ValidationGroup="Edit" Display="None" />

      <asp:ListView ID="ListView1" runat="server" DataSourceID="LinqDataSource1"
        InsertItemPosition="LastItem">
        <LayoutTemplate>
          <table cellpadding="2" border="1" runat="server" id="tblCustomers">
            <tr runat="server">
              <th runat="server">&nbsp;</th>              
              <th runat="server">Name</th>
              <th runat="server">Number</th>
              <th runat="server">Standard Cost</th>
              <th runat="server">List Price</th>
              <th runat="server">Sell Start Date</th>
            </tr>
            <tr runat="server" id="itemPlaceholder" />
          </table>
          <asp:DataPager runat="server" ID="CustomersPager" PageSize="20">
            <Fields>
              <asp:NumericPagerField ButtonCount="10" />
            </Fields>
          </asp:DataPager>
        </LayoutTemplate>
        <ItemTemplate>
          <tr runat="server">
            <td>
              <asp:LinkButton ID="EditButton" runat="server" CommandName="Edit" 
                Text="Edit" CausesValidation="false" />
              <asp:LinkButton ID="DeleteButton" runat="server" CommandName="Delete" 
                Text="Delete" CausesValidation="false" 
                OnClientClick='return confirm("Are you sure you want to delete this item?");' />
            </td>
            <td>
              <asp:DynamicControl runat="server" DataField="Name" />
            </td>
            <td>
              <asp:DynamicControl runat="server" DataField="ProductNumber" />
            </td>
            <td align="right">
              <asp:DynamicControl runat="server" DataField="StandardCost" DataFormatString="{0:C}" />
            </td>
            <td align="right">
              <asp:DynamicControl runat="server" DataField="ListPrice" DataFormatString="{0:C}" />
            </td>
            <td align="center">
              <asp:DynamicControl runat="server" DataField="SellStartDate" 
                DataFormatString="{0:MM/dd/yyyy}" NullDisplayText="&nbsp;" />
            </td>
          </tr>
        </ItemTemplate>
        <EditItemTemplate>
          <tr>
            <td>
              <asp:LinkButton ID="UpdateButton" runat="server" CommandName="Update" 
                Text="Update" ValidationGroup="Edit" />
              <asp:LinkButton ID="CancelButton" runat="server" CommandName="Cancel" 
                Text="Cancel" CausesValidation="false" />
            </td>
            <td>
              <asp:DynamicControl runat="server" DataField="Name" Mode="Edit" ValidationGroup="Edit" />
            </td>
            <td>
              <asp:DynamicControl runat="server" DataField="ProductNumber" Mode="Edit" ValidationGroup="Edit" />
            </td>
            <td>
              <asp:DynamicControl runat="server" DataField="StandardCost" Mode="Edit" ValidationGroup="Edit" />
            </td>
            <td>
              <asp:DynamicControl runat="server" DataField="ListPrice" Mode="Edit" ValidationGroup="Edit" />
            </td>
            <td>
              <asp:DynamicControl runat="server" DataField="SellStartDate" Mode="Edit" 
                ValidationGroup="Edit" DataFormatString="{0:MM/dd/yyyy}" ApplyFormatInEditMode="true" />
            </td>
          </tr>
        </EditItemTemplate>
        <InsertItemTemplate>
          <tr>
            <td>
              <asp:LinkButton ID="InsertButton" runat="server" CommandName="Insert" 
                Text="Insert" ValidationGroup="Insert" />
              <asp:LinkButton ID="CancelButton" runat="server" CommandName="Cancel" 
                Text="Cancel" CausesValidation="false" />
            </td>
            <td>
              <asp:DynamicControl runat="server" DataField="Name" Mode="Insert" ValidationGroup="Insert" />
            </td>
            <td>
              <asp:DynamicControl runat="server" DataField="ProductNumber" Mode="Insert" ValidationGroup="Insert" />
            </td>
            <td>
              <asp:DynamicControl runat="server" DataField="StandardCost" Mode="Insert" ValidationGroup="Insert" />
            </td>
            <td>
              <asp:DynamicControl runat="server" DataField="ListPrice" Mode="Insert" ValidationGroup="Insert" />
            </td>
            <td>
              <asp:DynamicControl runat="server" DataField="SellStartDate" Mode="Insert" ValidationGroup="Insert" />
            </td>
          </tr>
        </InsertItemTemplate>
      </asp:ListView>

      <!-- This example uses Microsoft SQL Server and connects   -->
      <!-- to the AdventureWorksLT sample database.              -->
      <asp:LinqDataSource ID="LinqDataSource1" runat="server" 
        TableName="Products"
        ContextTypeName="AdventureWorksLTDataContext"
        EnableUpdate="true"
        EnableDelete="true"
        EnableInsert="true" >
      </asp:LinqDataSource>

    </div>
    </form>
</body>
</html>

System.Object
  System.Web.UI.Control
    System.Web.DynamicData.DynamicControl

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

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.

.NET Framework

Supported in: 3.5 SP1

Date

History

Reason

July 2008

Added topic for new class.

SP1 feature change.

Community Additions

ADD
Show:
© 2014 Microsoft