DynamicControl Web Server Control Declarative Syntax

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

The DynamicControl control is part of the ASP.NET Dynamic Data framework. The control displays the content for a specified field in templated data-bound controls (such as FormView or ListView controls) by using Dynamic Data features such as field templates.

<asp:DynamicControl
    ApplyFormatInEditMode="True|False"
    ConvertEmptyStringToNull="True|False"
    CssClass="string"
    DataField="string"
    DataFormatString="string"
    EnableViewState="True|False"
    HtmlEncode="True|False"
    ID="string"
    Mode="Edit|Insert|ReadOnly"
    NullDisplayText="string"
    OnDataBinding="DataBinding event handler"
    OnDisposed="Disposed event handler"
    OnInit="Init event handler"
    OnLoad="Load event handler"
    OnPreRender="PreRender event handler"
    OnUnload="Unload event handler"
    runat="server"
    UIHint="string"
    ValidationGroup="string"
    Visible="True|False"
>
</asp:DynamicControl>

Remarks

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

The DynamicControl control provides functionality like 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, because you can also add HTML markup to the template.

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 that are displayed when the data-bound control is in edit mode, set the ApplyFormatInEditMode property to true. If a field value is null, you can display custom text by setting the NullDisplayText property. If you set the ConvertEmptyStringToNull property to true, the DynamicControl control automatically converts empty string ("") field values to null values.

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
    Specifies display mode, which prevents the user from modifying the values of a record or a data field. This mode can be used in any template.

  • Edit
    Specifies edit mode, which enables users to update values in an existing record. This mode is used in the EditItemTemplate template.

  • Insert
    Specifies insert mode, which enables users to enter values for a new record. This mode is used in the InsertItemTemplate template.

Example

The following example shows how to use the DynamicControl control in a ListView control to display and edit values from a database table.

<%@ Page Language="VB" %>

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

<script runat="server">

  Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs)    
    DynamicDataManager1.RegisterControl(ListView1)
  End Sub

</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>
<%@ 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>

See Also

Reference

DynamicControl