Sdílet prostřednictvím


How to: Customize the Layout of an Individual Table By Using a Custom Page Template

When you use ASP.NET Dynamic Data, you can use two general approaches to define custom behavior for a table in the data model. You can customize field templates to define field-specific behavior, and you can customize page templates or entity templates to define table-specific behavior.

To customize data fields in a table by using custom field templates, you first create a partial class whose name matches the entity class that represents the table. In the partial class, you can use the UIHintAttribute attribute to specify the name of the custom field template to use. For more information, see How to: Add Dynamic Behavior to Data-Bound Controls.

You can customize entity templates to define table specific behavior. These templates give you more control over the markup that is generated than the markup generated by using page templates. For more information, see Walkthrough: Customizing Table Layout Using Entity Templates.

To customize table behavior by using page templates, you create a subfolder in the DynamicData\CustomPages folder. The name of the subfolder matches the table accessor in the data-context class of the table you want to customize. You then create a custom page template in the folder. This topic shows you how to use this second approach.

The default page templates work for all tables and does not use schema-specific information. However, you can use schema information in order to display specific fields when you use custom page templates for specific tables.

To create a custom page template

  1. In Visual Studio or Visual Web Developer Express, create or open a Dynamic Data Web site. For more information about how to create a Dynamic Data Web site, see Walkthrough: Creating a New Dynamic Data Web Site Using Scaffolding.

  2. In the DynamicData\CustomPages folder, create a subfolder with the name of the entity set representing the table. For the folder name, use these guidelines:

    • If the data model is based on LINQ to SQL or ADO.NET Entity Framework 4 using the default "Pluralize or singularize generated object names", the entity set representing the table will generally have a plural form. For example, to create a custom page for the Product table in the AdventureWorksLT database, create a directory named DynamicData\CustomPages\Products. Check the data model for the name of entity set representing the table.

    • If the data model is based on the ADO.NET Entity Framework 3.5 or you changed the default "Pluralize or singularize generated object names", the entity set representing the table will have the same name as the table.

  3. Copy an existing page template from the DynamicData\PageTemplates folder to the subfolder that you created in the previous step.

    For example, copy DynamicData\PageTemplates\List.aspx to DynamicData\CustomPages\Products.

  4. If you created a Web application project (WAP) you must change the namespace in the copied page template to avoid compile errors due to a name collision. You can omit this step if you created a file based web site. For example, change the namespace in the DynamicData\CustomPages\Products\List.aspx file and in the corresponding code-behind file (List.aspx.vb or List.aspx.cs).

    For example, if you created a Web project with the name WS (and the namespace WS), add the namespace __Products to the DynamicData\PageTemplates\List.aspx code-behind file and in the DynamicData\PageTemplates\List.aspx file. The string you use to change the namespace is arbitrary.

    Note

    A WS namespace is created for the application. This is explicitly declared in the C# file, but is hidden in the Visual Basic file.

    The following example shows the code-behind file for List.aspx with the changed namespace.

    Alternatively, for a WAP, create a new List.aspx page in the DynamicData\CustomPages\Products folder. The new page will have the correct namespace in the @Page directive and in the code behind file. Copy the contents below the @Page directive from the DynamicData\PageTemplates\List.aspx to DynamicData\CustomPages\Products\List.aspx file. Copy the content of the DynamicData\PageTemplates\List.aspx code behind file to the DynamicData\CustomPages\Products\List.aspx code behind file while retaining the namespace generated in the DynamicData\CustomPages\Products\List.aspx code behind file.

    Namespace _Products
    
        Partial Class List
            Inherits System.Web.UI.Page
            ' List.aspx.vb contents omitted.
        End Class
    End Namespace 
    
    namespace WS {
        namespace _Product {
            public partial class List : System.Web.UI.Page {
                // List.aspx.cs contents omitted.
            }
        }
    } 
    

    The following example shows the List.aspx file with the changed namespace.

    [Visual Basic]

    <%@ Page Language="VB" MasterPageFile="~/Site.master" CodeBehind="List.aspx.vb" Inherits="WS._Products.List" %>
    
    <%@ Page Language="C#" MasterPageFile="~/Site.master" CodeBehind="List.aspx.cs" Inherits="WS._Product.List" %>
    
  5. Add your customizations to the page template that you copied earlier.

    The following example shows markup for a heading that has been changed.

    <h2> Custom Pages Demo <%= table.DisplayName%></h2>
    
  6. In the browser, browse the table and confirm that the template includes your changes.

    For example, browse the Product table. If you changed the heading as shown in the previous example, you see "Custom Pages Demo Products".

See Also

Tasks

Walkthrough: Creating a New Dynamic Data Web Site Using Scaffolding

Concepts

ASP.NET Dynamic Data Scaffolding