Share via


How to: Add Dynamic Behavior to ListView and FormView Controls

This topic describes how to add ASP.NET Dynamic Data behavior to the ListView and FormView controls by using the DynamicControl control. The DynamicControl class is used by the data-bound controls to display data fields that use Dynamic Data features.

In order to add dynamic behavior to the ListView and FormView controls, you must have the following:

A Visual Studio project with source code is available to accompany this topic: Adding Dynamic Data.

To add dynamic behavior

  1. In Visual Studio 2010 or Visual Web Developer 2010 Express, open the Web site that you want to add Dynamic Data behavior to.

  2. Open an ASP.NET Web page that contains an EntityDataSource control or the LinqDataSource control.

  3. Add a ListView or FormView control to the page.

  4. Add a DynamicDataManager control to the page.

  5. Set the ControlID property of the DataControlReference element in the DynamicDataManager control to the ID of the ListView or FormView control.

    This registers the data-bound control and enables dynamic behavior for it.

  6. Set the DataSourceID property of the ListView or FormView control to the ID of the EntityDataSource control or the LinqDataSource control.

  7. For each column in the ListView control or row in the FormView control that you want to enable data field display for, do the following:

    1. Add a DynamicControl control to the ItemTemplate template of the data-bound control.

    2. Set the DataField property of the DynamicControl control to the name of the data column or row that you want to bind to.

  8. For each column in the ListView control or row in the FormView control that you want to enable data field editing for, do the following:

    1. Add a DynamicControl control to the EditItemTemplate template of the data-bound control.

    2. Set the DataField property of the DynamicControl control to the name of the data column or row that you want to bind to.

    3. Set the Mode property to the Edit.

Example

The following examples show two pages that enable Dynamic Data behavior for the ListView and FormView control by using the DynamicControl control.

You can use these pages in a Web site with or without scaffolding. In either case, you must register the context for the data model. The data model can be either the Entity Framework or the LINQ-to-SQL model.

        <asp:ListView ID="ListView1" runat="server" DataKeyNames="CustomerID"  
            DataSourceID="EntityDataSource1">

            <EditItemTemplate>
                <tr style="">
                    <td>
                        <asp:LinkButton ID="UpdateButton" runat="server" CommandName="Update" 
                         CausesValidation="true" Text="Update" />
                        <asp:LinkButton ID="CancelButton" runat="server" CommandName="Cancel"  
                        CausesValidation="false" Text="Cancel" />
                    </td>


                    <td>
                        <asp:DynamicControl runat="server" DataField="FirstName" Mode="Edit" />
                    </td>

                    <td>
                        <asp:DynamicControl runat="server" DataField="LastName" Mode="Edit" />
                    </td>

                    <td>
                        <asp:DynamicControl runat="server" DataField="CompanyName" Mode="Edit" />
                    </td>

                    <td>
                        <asp:DynamicControl runat="server" DataField="EmailAddress" Mode="Edit" />
                    </td>

                    <td>
                        <asp:DynamicControl runat="server" DataField="ModifiedDate" Mode="Edit" />
                    </td>

                </tr>
            </EditItemTemplate>
            <EmptyDataTemplate>
                <table runat="server" style="">
                    <tr>
                        <td>
                            No data was returned.</td>
                    </tr>
                </table>

            </EmptyDataTemplate>

            <ItemTemplate>
                <tr style="">
                    <td>

                        <asp:LinkButton ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
                        <asp:LinkButton ID="LinkButton2" runat="server" CommandName="Delete" Text="Delete"
                            OnClientClick='return confirm("Are you sure you want to delete this item?");' />

                    </td>


                    <td>
                        <asp:DynamicControl runat="server" DataField="FirstName" Mode="ReadOnly" />
                    </td>

                    <td>
                        <asp:DynamicControl runat="server" DataField="LastName" Mode="ReadOnly" />
                    </td>

                    <td>
                        <asp:DynamicControl runat="server" DataField="CompanyName" Mode="ReadOnly" />
                    </td>

                    <td>
                        <asp:DynamicControl runat="server" DataField="EmailAddress" Mode="ReadOnly" />
                    </td>

                    <td>
                        <asp:DynamicControl runat="server" DataField="ModifiedDate" Mode="ReadOnly" />
                    </td>

                </tr>
            </ItemTemplate>
            <LayoutTemplate>
                <table  class="ExampleView" runat="server">
                    <tr runat="server">
                        <td runat="server">
                            <table ID="itemPlaceholderContainer" runat="server" border="0" style="">
                                <tr runat="server" style="">
                                    <th runat="server">
                                    </th>

                                    <th runat="server">
                                        FirstName</th>

                                    <th runat="server">
                                        LastName</th>

                                    <th runat="server">
                                        CompanyName</th>

                                    <th runat="server">
                                        EmailAddress</th>

                                    <th runat="server">
                                        ModifiedDate</th>

                                </tr>
                                <tr ID="itemPlaceholder" runat="server">
                                </tr>
                            </table>

                        </td>
                    </tr>
                    <tr runat="server">
                        <td runat="server" style="">
                            <asp:DataPager ID="DataPager1" runat="server">
                                <Fields>
                                    <asp:NumericPagerField />
                                </Fields>
                            </asp:DataPager>
                        </td>
                    </tr>
                </table>

            </LayoutTemplate>
            <SelectedItemTemplate>
                <tr style="">
                    <td>

                        <asp:LinkButton ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
                        <asp:LinkButton ID="LinkButton2" runat="server" CommandName="Delete" Text="Delete"
                            OnClientClick='return confirm("Are you sure you want to delete this item?");' />
                    </td>

                    <td>
                        <asp:DynamicControl runat="server" DataField="FirstName" Mode="ReadOnly" />
                    </td>

                    <td>
                        <asp:DynamicControl runat="server" DataField="LastName" Mode="ReadOnly" />
                    </td>

                    <td>
                        <asp:DynamicControl runat="server" DataField="CompanyName" Mode="ReadOnly" />
                    </td>

                    <td>
                        <asp:DynamicControl runat="server" DataField="EmailAddress" Mode="ReadOnly" />
                    </td>

                    <td>
                        <asp:DynamicControl runat="server" DataField="ModifiedDate" Mode="ReadOnly" />
                    </td>


                </tr>
            </SelectedItemTemplate>
        </asp:ListView>

              <asp:FormView ID="FormViewId" runat="server" AllowPaging="True"  CssClass="DDDetailsTable"
                  DataSourceID="EntityDataSource1">
                  <EditItemTemplate>
                      FirstName:
                      <asp:DynamicControl ID="FirstNameDynamicControl" runat="server" 
                          DataField="FirstName" Mode="Edit" />
                      <br />
                      LastName:
                      <asp:DynamicControl ID="LastNameDynamicControl" runat="server" 
                          DataField="LastName" Mode="Edit" />
                      <br />
                      CompanyName:
                      <asp:DynamicControl ID="CompanyNameDynamicControl" runat="server" 
                          DataField="CompanyName" Mode="Edit" />
                      <br />
                      EmailAddress:
                      <asp:DynamicControl ID="EmailAddressDynamicControl" runat="server" 
                          DataField="EmailAddress" Mode="Edit" />
                      <br />
                      ModifiedDate:
                      <asp:DynamicControl ID="ModifiedDateDynamicControl" runat="server" 
                          DataField="ModifiedDate" Mode="Edit" />
                      <br />
                      <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" 
                          CommandName="Update" Text="Update" />
                      &nbsp;<asp:LinkButton ID="UpdateCancelButton" runat="server" 
                          CausesValidation="False" CommandName="Cancel" Text="Cancel" />
                  </EditItemTemplate>

                  <ItemTemplate>
                  <table  class="DDDetailsTable">
                    <tr> 
                        <td> FirstName:</td>
                        <td> <asp:DynamicControl ID="FirstNameDynamicControl" runat="server" 
                          DataField="FirstName" /></td> 
                    </tr>

                    <tr>
                        <td> LastName: </td>
                        <td> <asp:DynamicControl ID="LastNameDynamicControl" runat="server" 
                          DataField="LastName" /></td>
                    </tr>                
                    <tr>
                        <td> Company Name: </td>
                        <td> <asp:DynamicControl ID="CompanyNameDynamicControl" runat="server" 
                          DataField="CompanyName" /> </td>
                    </tr>
                    <tr>
                        <td>EmailAddress:</td>
                        <td><asp:DynamicControl ID="EmailAddressDynamicControl" runat="server" 
                            DataField="EmailAddress" /></td>
                    </tr>
                    <tr>
                        <td> Modified Date:</td>
                        <td><asp:DynamicControl ID="ModifiedDateDynamicControl4" runat="server" 
                          DataField="ModifiedDate" /></td>
                    </tr>

                  </table>


                  <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Edit" Text="Edit" /> &nbsp;
                  <asp:LinkButton ID="LinkButton2" runat="server" CommandName="Delete" Text="Delete"
                    OnClientClick='return confirm("Are you sure you want to delete this item?");' />

                  </ItemTemplate>

              </asp:FormView>

Compiling the Code

This example requires the following:

See Also

Concepts

ASP.NET Dynamic Data Overview

ASP.NET Dynamic Data Scaffolding

Customizing ASP.NET Dynamic Data

LinqDataSource Web Server Control Overview

Other Resources

EntityDataSource Web Server Control Overview