Del via


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

ASP.NET Dynamic Data provides two general approaches for how to define custom behavior for a table in the data model. The first approach is to customize data fields in a table by creating custom field templates. You then 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 that is used for display. For more information, see How to: Customize Data Field Appearance and Behavior in a Dynamic Data Control.

The second approach is to customize the data table by creating a custom page template. You put custom page template in a subfolder in the DynamicData\CustomPages folder. The name of the subfolder matches the table accessor in the data context class of the table that you are customizing. This topic shows you how to use this second approach.

The default page templates work for all tables and do 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. Make sure that scaffolding is enabled. In the Global.asax file, set the ContextConfiguration.ScaffoldAllTables property to true in the MetaModelRegisterContext() method.

    The following example shows the RegisterRoutes method that includes a call to enable scaffolding and to enable the List action.

    Public Shared Sub RegisterRoutes(ByVal routes As RouteCollection)
        Dim model As New MetaModel
        model.RegisterContext(GetType(AdventureWorksLTDataContext), _
            New ContextConfiguration() With {.ScaffoldAllTables = True})
    
        routes.Add(New DynamicDataRoute("{table}/{action}.aspx") With { _
             .Constraints = New RouteValueDictionary(New With _
                {.Action = "List|Details|Edit|Insert"}), _
                 .Model = model})
    
    End Sub
    
    public static void RegisterRoutes(RouteCollection routes) {
         MetaModel model = new MetaModel();
    
        model.RegisterContext(typeof(AdventureWorksLTDataContext), 
            new ContextConfiguration() { ScaffoldAllTables = true });
    
         routes.Add(new DynamicDataRoute("{table}/{action}.aspx")
         {
             Constraints = new RouteValueDictionary(new { action = 
                "List|Details|Edit|Insert" }),
                Model = model
            });
        }
    

    For more information about how to enable scaffolding, see Walkthrough: Creating a New ASP.NET Dynamic Data Web Site Using Scaffolding.

  2. In the DynamicData\CustomPages folder, create a subfolder. For the folder name, use these guidelines:

    • If the data model is based on LINQ to SQL, use the table name in the data context as the folder name. For example, to create a custom page for the Product table in the AdventureWorksLT database, create a directory named DynamicData\CustomPages\Products.

    • If the data model is based on the ADO.NET Entity Framework, use the table name in the object context as the folder name. For example, to create a custom page for the Product table in the AdventureWorksLT database, create a directory named DynamicData\CustomPages\Product.

  3. Copy an existing page template from the DynamicData\PageTemplates folder to the new subfolder.

    For example, copy DynamicData\PageTemplates\List.aspx to DynamicData\CustomPages\Products (for LINQ to SQL) or to DynamicData\CustomPages\Product (for the Entity Framework).

  4. Customize the page template that you copied in the previous step.

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

    <h2> Custom Pages Demo <%= table.DisplayName%></h2>
    
  5. Test the custom template by displaying the table in a browser and confirming that the template includes your changes.

    For example, display the Product table in the browser. You will see "Custom Pages Demo Product".

    Note

    One reason that a custom page might not render is if the subfolder name for the custom page template does not match an entity name. For example, when you use LINQ to SQL with the Product table in the AdventureWorksLT database, the subfolder name must be Products (plural). The same custom page created for the ADO.NET Entity Framework must be in a folder that is named Product (singular).

See Also

Tasks

Walkthrough: Creating a New ASP.NET Dynamic Data Web Site Using Scaffolding

Concepts

ASP.NET Dynamic Data Scaffolding and Page Templates Overview

Other Resources

Using ASP.NET Dynamic Data