AJAX Quickstarts   |   Silverlight Quickstarts   |   Dynamic Data Controls Quickstarts   |   Services Quickstarts   |   Dynamic Languages Quickstarts   |   ASP.NET AJAX Home   |      |  I want my samples in...     

ASP.NET Futures (2007): Introduction to ASP.NET Dynamic Data Controls

This section introduces dynamic data controls, a set of ASP.NET controls that obtain schema information at run time, provide default display formats according to common user expectations, and enable you to easily customize those formats. The section covers the following subjects:

  • How dynamic data controls work.

  • Dynamic data controls provided in the Futures release.

  • Creating a master/details page using dynamic data controls.

  • Customizing the behavior of dynamic data controls.

Dynamic data controls simplify the creation of data-driven Web applications by providing a sensible set of default display formats, based on common user expectations. PuttingGood together a data-driven Web page requires minimal information from the user and does not rely on designer-generated files.

The key to this simplicity is that dynamic data controls target very common scenarios for data-driven applications and make those common cases very simple. By discovering database schema information at run time, dynamic data controls can make very good guesses about what the user probably wants to do. However, dynamic data controls also enable you to easily add customization for uncommon cases.

How Dynamic Data Controls Work

You use dynamic data controls by placing them on a dynamic data Web page in a dynamic data Web site. Templates for the Web site and page are available in Visual Studio or Visual Web Developer. The dynamic data Web page template is available in the Add New Item dialog box, and the dynamic data Web site template is available when you add a new Web site.

Dynamic data Web pages are bound to database tables by matching names. There is a one-to-one mapping between pages and tables (or views). By default, the name of the page identifies the table or view that the page displays. For the default naming convention, there is no need for configuration; the dynamic data controls retrieve schema information at run time and display the data.

Simple Usage Scenario

The simplest way to use dynamic data controls is to add a DynamicAutoData control to a page. The following example shows the markup for a DynamicAutoData control.

<asp:DynamicAutoData runat="server" />

When the page runs, the DynamicAutoData control renders a set of dynamic data controls that display the table or view whose name matches the page. The controls enable users to insert, update, and delete records, filter the data, view a detail record, and obtain an RSS link to the table. For details about the controls included in this default user interface, see the DynamicAutoData control.

How Dynamic Data Controls Locate the Database

Dynamic data pages and controls use the following logic to determine which database to open:

  1. If there is a connection string specified in the <dynamicDataControls> section of the Web.config file, that connection string is used. The following example shows a <dynamicDataControls> section that specifies a connection string.
    <dynamicDataControls showAllTables="true"
            dataLayerType="Microsoft.Web.DynamicDataControls.SqlDataLayer"
            connectionString="TASKSConnectionString" > 
        <nameMap>
        </nameMap>
    </dynamicDataControls>
  2. If the <dynamicDataControls> section of the Web.config file does not specify a connection string, and the Web.config file contains a single connection string, that string is used. If there are multiple connection strings, you must specify a <dynamicDataControls> section.
  3. If there are no connection strings in the Web.config file, and there is a database (for example, an .mdf file) in the App_Data folder, that database is used. In this case, no configuration is needed. If there are multiple databases, you must provide at least one connection string.

The dynamic data controls do not search multiple databases for a table or view that matches a page name. If there are multiple databases or connection strings to choose from, you must specify a <dynamicDataControls> section in the Web.config file.

How Dynamic Data Controls Are Mapped to a Table or View

By default, dynamic data controls use the table or view with the same name as the page that contains the controls. For example, if you have a table named Customers, you can create a page named Customers.aspx in the root folder of your application.

If you want to have several pages that present the same table in different ways (for example, with different selections of columns), you can create a folder that is named for the table and put multiple pages in it. For example, the dynamic data controls in the following .aspx files all use the Customers table:

~/Customers/list.aspx
~/Customers/details.aspx
~/Customers/anotherCustomView.aspx

Note  The page names List.aspx and Details.aspx have special meaning for dynamic data controls. These names are used for navigation between tables.

You can override the default table mapping by creating a <nameMap> section in the Web.config file. For example, the following entry specifies that the views for the Customers table are in the folder MyCustFolder instead of in the Customers folder.

<dynamicDataControls listView="MyList" detailsView="MyDetails">
    <nameMap>
        <add table="customers" pathPrefix="~/MyCustFolder" />
    </nameMap>
</dynamicDataControls>

The configuration file entry also uses the listView and detailsView attributes to specify that the default list page in any folder must be named MyList.aspx instead of List.aspx, and that the default details page must be named MyDetails.aspx instead of Details.aspx.

Dynamic Data Controls Provided in the Futures Release

The following topics provide details about the dynamic data controls.

  • DynamicAutoData control. Displays the current table as a GridView control and displays user interface elements to select which rows of the table are displayed, to view details of a row, to insert a new row, and so on.
  • DynamicList control. Displays the current table as a GridView control.
  • DynamicDetails control. Displays the columns of a row in the current table.
  • DynamicFilter control. Enables the user to select which rows of the current table to display, based on the values of a column in the current table.
  • DynamicInsert control. Enables users to insert new rows into the current table.
  • DynamicNavigator control. Displays a list of all the tables and views that are currently accessible through dynamic data controls.
  • DynamicRssLink control. Serves data on the current page as an RSS feed.

Creating a Master/Details Page

You can create a master/details page by placing a DynamicList control and a DynamicDetails control on a page. The presence of the DynamicDetails control automatically makes the list selectable, and the details of the currently selected row are automatically displayed in the DynamicDetails control.

By default, the DynamicDetails control displays all the columns in the table, even if you have written a GetColumns method to restrict or rearrange the columns displayed by the control. You can customize this behavior by adding a GetDetailColumns method to the page, in exactly the same way that you would add a GetColumns method. The GetColumns method is described in the next section.

Customizing the Behavior of Dynamic Data Controls

Dynamic data controls are designed to have intuitive default behavior, but also to be easy to customize. You can customize the way the data in a table or view is displayed and the way rows are selected for display.

Customizing Data Display by Using the Extender Pattern

By default, dynamic data contols display data by using a standard set of ASP.NET controls with predefined settings. For example, the DynamicList control creates a GridView control, and the DynamicDetails control creates a DetailsView control. The extender pattern enables you to customize the display by specifying that a dynamic data control must instead use a control that you specify. In the following example, a DynamicFilter control is customized by specifying a drop-down list control with a custom background color and font style. In the DynamicFilter control, the ControlID attribute specifies that the drop-down list on the page should be used instead of the default drop-down list.

<asp:DropDownList runat="server" ID="ddl"
      BackColor="Yellow" Font-Italic="true" />
<asp:DynamicFilter runat="server" ColumnName="Category" ControlID="ddl" />

Similarly, you can supply your own GridView control to use with a DynamicList control, your own DetailsView control to use with a DynamicDetails or DynamicInsert control, your own HyperLink control to use with a DynamicRssLink control, and your own Menu control to use with a DynamicNavigator control.

Choosing the Columns to Display

You can specify what columns to display by creating a method named GetColumns. The method must return an array that contains the names of the columns you want to display, in the order in which they are to be displayed. For example, the following method displays the Description and Complete columns from the current table or view.

		
public override IEnumerable GetColumns()
{
    return new object[] { "Description", "Complete" };
}
C#

This method and the others described in this section are methods of the DynamicDataPage class. When a Web site is enabled for dynamic data controls, the DynamicDataPage class is used as the base class for all pages. For statically compiled languages like C# and Visual Basic, you must explicitly override the methods. For dynamic languages, the dynamic data control framework handles the mapping.

Creating Custom Columns

You can create custom columns by including a lambda expression, anonymous function, or anonymous method in the array returned by the GetColumns method. For example, the following code displays the Category column and a computed column whose value is the Description column followed by parentheses enclosing the value of the Complete column.

		
public override IEnumerable GetColumns()
{
    return new object[] { 
        "Category",
        new DynamicDataColumn(
            delegate { return EvalS("ShipName") + " (" + 
                EvalS("ShipAddress") + ")"; })
    };
}
C#

By default, the custom column has the title Custom Column. To provide a different title, use the DynamicDataColumn constructor that takes a string representing the title and an anonymous method. In IronPython, this is achieved by passing a list containing the title and a lambda expression. In Managed JScript, pass an array containing the title and an anonymous function. The following example shows how to customize the column title.

		
public override IEnumerable GetColumns()
{
    return new object[] { 
        "Category",
        new DynamicDataColumn(
            "Description (Complete)",
            delegate { return EvalS("ShipName") + " (" + 
                EvalS("ShipAddress") + ")"; }
        )
    };
}
C#

Customizing Behavior by Overriding Other Methods

The GetColumns method enables you to customize how columns are displayed in the DynamicList control. However, it does not affect the DynamicDetails control, which by default shows all columns in the table or view. You can change this by providing a GetDetailColumns method, which is similar to the GetColumns method.

You can customize the Title and Description elements of RSS items in a similar way. You supply a GetRSSFields method that returns an array containing the two values. The following example shows a customized RSS feed.

		
public override IEnumerable GetRSSFields()
{
    return new object[] { "TitleColumn", "DescriptionColumn" };
}
C#

Customizing Row Display

To customize the way rows in the table are displayed, supply a method named InitRow. The method takes one parameter, which is the row to be customized. The method is called for each row in the table. For example, the following method sets the background color for each row to yellow if the Description column contains the word "Buy".

		
public override void InitRow(GridViewRow row)
{
    if (EvalS("ShipName").IndexOf("Buy") > -1)
    {
        row.BackColor = Color.Yellow;;
    }
}
C#

Customizing the Data Source

You can customize the data source by supplying a method named InitDataSource. The method takes one parameter, which is the data source. The method is called with the data source for the current table or view. For example, the following method sets the filter expression for the data source.

		
public override void InitDataSource(IDataSource dataSource)
{
    dataSource.FilterExpression = ...;
}
C#


This topic is Microsoft ASP.NET Futures pre-release documentation and is unsupported by Microsoft. Blank topics are included as placeholders and existing content is subject to change in future releases.