Friday 15 October 2010

Dynamic Data Features

How to add Dynamic Data to an existing site

1.       Copy ‘DynamicData’ folder, site.css, and site.master files.
2.       Add references to ‘System.ComponentModel.DataAnnotations’, ‘System.Web.Routing’, and ‘System.Web.DynamicData’.
3.  Modify global.asax file
protected void Application_Start(object sender, EventArgs e)
        {
            // Create an instance of the data model.
            MetaModel DefaultModel = new MetaModel();
            // Register the data model.
            DefaultModel.RegisterContext(typeof(
              AdventureWorksLT_DataEntities),
              new ContextConfiguration() { ScaffoldAllTables = false });

            // Create the routes.     
            RouteTable.Routes.Add(new
                DynamicDataRoute("{table}/{action}.aspx")
                {
                    Constraints = new RouteValueDictionary(new
                    {
                        action = "List|Details|Edit|Insert"
                    }),
                    Model = DefaultModel
                });
        }


Adding Dynamic Behaviour to Controls

1.       For GridView and DetailsView that uses LinqDataSource or EntityDataSource:
use ‘DynamicField
<asp:DynamicField DataField="OrderQty" HeaderText="Order Quantity" />For sample code see ‘addDDBehaviourToGridViewAndDetailsVew.aspx’
2.       For ListView and FormView that uses LinqDataSource or EntityDataSource:
use ‘DynamicControl
<asp:DynamicControl ID="DynamicControl1" runat="server" DataField="FirstName" Mode="Edit" /> For sample code see ‘addDDBehaviourToListViewAndFormView.aspx’

Action Links

-          Data-bound control table row action links
use ‘DynamicHyperLink’ control
<asp:TemplateField>
    <ItemTemplate>                             
        <asp:DynamicHyperLink runat="server" ID="EditHyperLink"
            Action="Edit" Text="Edit"/>
    </ItemTemplate>
</asp:TemplateField>

-          Unbound action links
<asp:DynamicHyperLink ID="InsertHyperLink" runat="server"
    Action="Insert" ContextTypeName="DynamicDataFeatures.AdventureWorksLT_DataEntities"  
    TableName="ProductModels">Insert new item
</asp:DynamicHyperLink>
<asp:DynamicHyperLink ID="DynamicHyperLink2" runat="server" Action="Edit"
    ContextTypeName="DynamicDataFeatures.AdventureWorksLT_DataEntities"
    TableName="ProductModels" ProductModelID="1">Edit item</asp:DynamicHyperLink>

For example, see ‘ActionLinks.aspx’


Customising Data Field Appearance and Behaviour

1.       Add custom field template files inside ‘FieldTemplates’ folder.
Add CustomFieldTemplateName.ascx for viewing and CustomFieldTemplateName_Edit.ascx for editing.
2.       Modify the markup and display functionality.
We may need to overwrite the ‘OnDataBinding’ method as well.
See ‘MyCustomFieldTemplate.ascx’ for more details.
3.  Refer to the custom field template in the databound row control by adding ‘UIHint
<asp:DynamicField DataField="OrderQty" HeaderText="Order Quantity" UIHint="MyCustomFieldTemplate" />
See ‘customDataField.aspx’ for more details.
To associate with an entity that will affect the rendering in the whole project, create a partial class for that entity then use ‘UIHint’ attribute on its meta data class.

Customising a Table Layout using Custom Page Template

1.       In the ‘DynamicData\CustomPages’ folder, create a subfolder with the name of the entity
2.       Copy an existing page template from the ‘DynamicData\PageTemplates’ folder to the subfolder created
3.       Change the copied files namespace if necessary
4.       Modify the new template
See ‘CustomPages/Products’ folder for more details.

Customising a Table Layout using Entity Template
If it’s only for editing details, edit, and insert views, we can use Entity Template.
1.       Add new ascx file with the entity name as the filename in the ‘EntityTemplates’ folder
2.       In the class file change the base class from ‘UserControl’ to ‘EntityTemplateUserControl
3.       Modify the new template file
See ‘Addresses.ascx’ inside ‘EntityTemplates’ folder.


Customising Entity Model Class

1.       Create a partial class for the entity
2.       Add references to System.Web.DynamicData and System.ComponentModel.DataAnnotations with ‘Using’ keyword
3.       Create a class that will act as the associated metadata class for the entity partial class
4.       Add ‘MetadataTypeAttribute’ attribute to the entity partial class definition with the class name that has just been created
[MetadataType(typeof(CustomerMetaData))]
public partial class Customer { }
5.       Add a property accessor for each data field that we want to provide attributes for. For any other fields that we want them to be displayed as default, ignore specifying them.
Sample:
[MetadataType(typeof(SalesOrderDetailMetadata))]
public partial class SalesOrderDetail
{
    public partial class SalesOrderDetailMetadata
    {
        [UIHint("MyCustomFieldTemplate")]
        public object OrderQty;

        [DataType(DataType.Date)]
        public object ModifiedDate { getset; }
    }
}

Some useful attributes:
-           [Required]
-           [Range(X, Y)]
-           [DataType(DataType.DataType)]
-           [ScaffoldColumn(true/false)]
-           [DisplayName(String)]
-           [RegularExpression(ExpressionErrorMessage = String)]
-           [ScaffoldTable(true)] – This attribute is used for an entity partial class, not a data field.



Modifying Field Template to Use Customised Data Attributes

For this example, see ‘Customer.cs’ and ‘FieldTemplates/Text.ascx.cs