Export (0) Print
Expand All

How to: Add Dynamic Behavior to GridView and DetailsView Controls

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

In order to add dynamic behavior to the GridView and DetailsView 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 to a GridView or DetailsView controls

  1. In Visual Studio 2010 or Visual Web Developer 2010 Express, open the Web site.

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

  3. Add a GridView or DetailsView 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 GridView or DetailsView control.

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

  6. Set the DataSourceID property of the GridView or DetailsView control to the ID of the EntityDataSource control or the LinqDataSource control.

  7. Set the AutoGenerateColumns property of the GridView control or the AutoGenerateRows property of the DetailsView control to false.

  8. For each column in the GridView control or row in the DetailsView control that you want to enable Dynamic Data behavior for, do the following:

    1. Add a DynamicField control.

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

The following examples show two pages that add Dynamic Data behavior to the GridView and DetailsView control by using the DynamicField 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:GridView ID="GridView1" runat="server" DataSourceID="GridDataSource" 
    EnablePersistedSelection="True" AutoGenerateColumns="False" GridLines="None" 
    AllowPaging="True" AllowSorting="True" CssClass="DDGridView"
    RowStyle-CssClass="td" HeaderStyle-CssClass="th" CellPadding="6">
    <Columns>
        <asp:CommandField ShowSelectButton="True" />
        <asp:TemplateField>
            <ItemTemplate>
                <asp:DynamicHyperLink runat="server" Action="Edit" Text="Edit"
                />&nbsp;<asp:LinkButton runat="server" CommandName="Delete" Text="Delete"
                    OnClientClick='return confirm("Are you sure you want to delete this item?");'
                />&nbsp;<asp:DynamicHyperLink runat="server" Text="Details" /> 

            </ItemTemplate>
        </asp:TemplateField>

        <asp:DynamicField DataField="OrderQty" HeaderText="Order Quantity" />
        <asp:DynamicField DataField="UnitPrice" HeaderText="Unit Price" />
        <asp:DynamicField DataField="ModifiedDate" HeaderText="Modified Date" />
        <asp:DynamicField DataField="SalesOrderHeader" HeaderText="Sales Order" />
        <asp:DynamicField DataField="Product" HeaderText="Product Name" />
    </Columns>
    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
    <HeaderStyle CssClass="th" />

    <PagerStyle CssClass="DDFooter"/>        
    <PagerTemplate>
        <asp:GridViewPager runat="server" />
    </PagerTemplate>
    <EmptyDataTemplate>
        There are currently no items in this table.
    </EmptyDataTemplate>
    <RowStyle CssClass="td" />
</asp:GridView>



<asp:DetailsView ID="DetailsView1" runat="server" AllowPaging="True" 
    CssClass="DDDetailsTable"
    AutoGenerateRows="False" DataSourceID="GridDataSource">

    <Fields>
        <asp:DynamicField DataField="OrderQty" HeaderText="Order Quantity" />
        <asp:DynamicField DataField="UnitPrice" HeaderText="Unit Price" />
        <asp:DynamicField DataField="ModifiedDate" HeaderText="Modified Date" />
        <asp:DynamicField DataField="Product" HeaderText="Product" />

        <asp:TemplateField ShowHeader="False" >
            <ItemTemplate>
                <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>

            <EditItemTemplate>
                <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" 
                    CommandName="Update" Text="Update"></asp:LinkButton> &nbsp;
                <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" 
                    CommandName="Cancel" Text="Cancel"></asp:LinkButton>
            </EditItemTemplate>
        </asp:TemplateField>

    </Fields>

</asp:DetailsView>


This example requires the following:

Community Additions

ADD
Show:
© 2014 Microsoft