Dynamic Data is an ASP.Net feature that allows easy creation of data-driven websites by automatically generating pages and user interfaces based on a data model. It infers the UI from metadata on the data model using LINQ to SQL or Entity Framework. Pages are generated dynamically using page and field templates. Customization is possible through custom page templates and field templates. A Dynamic Data Wizard is in development to further streamline website creation but is currently in beta.
Streamlining Python Development: A Guide to a Modern Project Setup
ASP.Net Dynamic Data Site
1. ASP.Net 3.5 SP1-
Dynamic Data
Michał Morciniec – GPSD Spain
micham@microsoft.com
2. Agenda
• Dynamic Data
– Concepts and Architecture
• Project creation in VS 2008 SP1
• Dynamic Data Website Elements
– Page Templates, Page Modes
– Field Tempates
• Customization
– Custom Page Templates, Custom Field Templates
– MetaData application, data validation
• Dynamic Data Wizard
3. Architecture
Page and Field Templates
Entities, Field,
Partial classes,
Classes, Metadata Attributes
Tables, columns,
constraints
LINQ to SQL
Page generated
dynamically
Data Source Data Model Data Model
Meta-data
application
4. Dynamic Data Concepts
• Makes easy to create data driven web sites
(operaciones CRUD)
• UI is inferred from the Data Model
• Data Model uses metadata to describe the structure
and data relationships.
• Basic data validation rules are imposed automatically
• Pages are generated automatically based on
templates
• URL Routing feature is used to map URLs to pages.
5. Dynamic Data WebSite – Creation 1
• Use template
“Dynamic Data Web
Site”
• Web Site Project
contains “Dynamic
Data” folder
6. Dynamic Data WebSite – Creation 2
• Add LINQ to SQL o
LINQ to Entity
classes to project
• Note - App_Code
folder is added with
the Data Model
(.dbml)
7. Dynamic Data WebSite – Creation 3
• Create data
connection, drag
tables to designer of
Data Model
• Data Model (classes )
is generated in “code-
behind” associatesd
with the model
(.designer.cs)
8. Dynamic Data WebSite – Creation 4
• Configure Data Model class (a.k.a.
DataContext) in Global.asax in
RegisterRoutes()
MetaModel model=new MetaModel();
model.RegisterContext(
typeof(NorthwindDataContext),
new ContextConfiguration()
{ ScaffoldAllTables = true }
);
10. Page Templates
• Located in Dynamic
DataPage Templates
• Page Uses Controls-
– DynamicDataManager
– DynamicValidator
– FilterRepeater
– DynamicControl
– DynamicField
• GridView used to show
data.
11. Page Modes
• Separate-Page Mode (default)
– Separate pages used to list, edit and insert new data
• Combined-Page Mode
– Data is manipulated in the same page
• Different Page Modes can be associated with
different tables (entities)
12. Separate-Page Mode
• Uses following route in Global.asax
– {table}/{action}.
• {action} can be
– Edit routes.Add(
– List new DynamicDataRoute(quot;{table}/{action}.aspxquot;)
{
– Details Constraints = new RouteValueDictionary(
new { action = quot;List|Details|Edit|Insertquot; }),
– Insert Model = model
});
• Every operation uses different page
– No “in-line” editing
13. Combined Page Mode
• Uses following routes in Global.asax
– {table}/ListDetails.aspx
• Same page ListDetails.aspx used to
manipulate data and show detail
– “in-line” editing
routes.Add(
new DynamicDataRoute(quot;{table}/ListDetails.aspxquot;)
{ Action = PageAction.List,
ViewName = quot;ListDetailsquot;,
Model = model
});
routes.Add(
new DynamicDataRoute(quot;{table}/ListDetails.aspxquot;)
{ Action = PageAction.Details,
ViewName = quot;ListDetailsquot;,
Model = model
});
14. Page Modes combination
• Each entity can use its Page Mode:
• Order is significant
routes.Add(
new DynamicDataRoute(“Products/{action}.aspxquot;)
{
Constraints = new RouteValueDictionary(
new { action = quot;List|Details|Edit|Insertquot; }),
Model = model,
Table = “Products”
});
routes.Add(
new DynamicDataRoute(quot;{table}/ListDetails.aspxquot;)
{ Action = PageAction.List,
ViewName = quot;ListDetailsquot;,
Model = model
});
routes.Add(
new DynamicDataRoute(quot;{table}/ListDetails.aspxquot;)
{ Action = PageAction.Details,
ViewName = quot;ListDetailsquot;,
Model = model
});
15. Field Templates
• Dynamic Data selects controls
based on field datatype
– Boolean shown as checkbox
– Relationships shown as drop down list
– Text show as TextBox or Literal
• Pages use Field Templates to
show control at runtime
– Controls do not form part of Page Template
markup, injected at runtime
• FieldTemplates are stored in
DynamicDataFieldTemplates
– “Intrinsic types”
16. Field Templates
• Fields with unknown datatype are not displayed
• Atribute UIHint associates control with datatype
– Substitute control for known datatype
– Assign control for “unknown” datatype
• Field data is displayed using markup
– <%# FieldValueString %>
– <%# FieldValueEditString %> (edit mode)
17. Custom Page Templates
• Page Templates can be designed
• They follow naming convention
– DynamicDataCustomPages{entity_name
}PageName.aspx
• To associate page template
ListDetails.aspx with table
Suppliers store it in
– DynamicDataCustomPagesSupplier
sListDetails.aspx
18. Custom Page Templates
• Tipically, page markup contains DynamicField control
– Concrete control for field is selected at runtime
<Columns>
<asp:DynamicField DataField=quot;CompanyNamequot; />
<asp:DynamicField DataField=quot;Phonequot; />
<asp:DynamicField DataField=quot;Faxquot; />
<asp:DynamicField DataField=quot;Productsquot; />
</Columns>
• Dynamic Data Engine maintains consistent URLs
– Name of the customized page has to coincide with the “built-in”
page names (ListDetails.aspx, Edit.aspx, List.aspx, etc.)
– Same URL used to access customized and uncustomized pages
19. Custom Field Template
• It is possible to modify control associated with a datatype
– Associate Calendar control with field of type Date
• New templates can be assigned to “unknown” datatypes
– Use Image control for binary field.
• Custom Field Templates extend FieldTemplate
UserControl
• Property FieldValue retrieves field data
<asp:Calendar ID=quot;Calendar1quot; runat=quot;server“
SelectedDate=quot;<%# (FieldValue!=null)? FieldValue:DateTime.Now %>quot;
VisibleDate=quot;<%# (FieldValue!=null)? FieldValue:DateTime.Now %>quot;
</asp:Calendar>
20. Custom Field Template-Edit Mode
• Use ExtractValues() to write control data to
DB
protected override void ExtractValues(IOrderedDictionary
dictionary) {
dictionary[Column.Name] =
ConvertEditedValue(Calendar1.SelectedDate.ToShortDateString());
}
21. MetaData application
• Dynamic Data uses MetaData specified in the Data Model
– But when model is regenerated changes are lost
– Define additional metadata in partial class
using System;
using System.ComponentModel.DataAnnotations;
[MetadataType(typeof(EmployeeMetadata))]
public partial class Employee
{
}
public class EmployeeMetadata
{
[UIHint(quot;DateTimeCalendarquot;)]
public object HireDate { get; set; }
}
22. Data Validation
• Can use metadata
public class CustomerMetadata {
– [Required()] [Required()]
public object Title;
}
– [Range(0,100)]
• Using validation events (Local)
• Pattern On<Field>Changing, On<Field>Changed
public partial class Customer {
partial void OnTitleChanging(string value) {
if (!Char.IsUpper(value[0])) {
throw new ValidationException(
quot;Title must start with an uppercase letter.quot;);
}
}
}
23. Data Validation
• Using Validate event (Global)
– OnValidate fires on change for any field
– You can filter on type on change
(ChangeAction)
public partial class Employee{
partial void OnValidate(System.Data.Linq.ChangeAction action){
if (action == System.Data.Linq.ChangeAction.Insert)) {
if (this._BirthDate.Value.CompateTo(DateTime.Now)>0)
throw new ValidationException(“The birth date cannot be
in the future”);
}
}
}
25. Dynamic Data Wizard
• Facilitates creation of Dynamic Data web sites
• In development
– Not part of the .Net 3.5 SP1 RTM (not supported)
– Downloadable from CodePlex
– Requires SP1 of VS 2008
– Will not work with VS 2008 Express Ed.
27. Step1: Data Model
• Data Model
– New Data Model generation
– Can select existing model
28. Step2: Choose Entities for the Model
• Table selection
• Can specify Namespace
• Can specify class name ( “DataContext” is appended)
• LINQ to SQL or LINQ to Entities
29. Step3: Page Template Selection
• Wizard generates Custom Page Templates
– Template for a list view (List)
– for details (Details), adds Details link for each element in
list view
– For edition view (Edit)
– New element (Insert)
30. Step4: Operation Customization
• Select/Restrict operations type
– edition, insertion, deletion, details
• You can select fields for each view
31. Dynamic Data Wizard - Summary
• Data Model is generated (DataContext) and the Page
Templates
• All Page Tempates go into CustomPages
– Wizard does not use “standard” page templates
• Can select Fields to appear in each View and types of
operations
33. Summary
• Dynamic Data facilitates creation of data-driven web
sites
– Increases development productivity
– Simplifies maintenance
– Possibility of customization
• Dynamic Data Wizard still under development
34. References
• Introduction to ASP.NET Dynamic Data-Dynamic Data in Action Webcasts
– http://www.asp.net/dynamicdata/
• Using ASP.NET Dynamic Data (MSDN)
– http://msdn.microsoft.com/en-us/library/cc488545.aspx
• Dynamic Data Futures (Source Code)
– http://www.codeplex.com/aspnet/Release/ProjectReleases.aspx?ReleaseId=14475
• Dynamic Data Wizard (Template for VS 2008 SP1)
– http://www.codeplex.com/aspnet/Release/ProjectReleases.aspx?ReleaseId=14474
• ASP .NET Dynamic Data (Video Links)
– http://www.myvbprof.com/2007_Version/Dynamic_Data_Tutorial.aspx
• ASP .NET Dynamic Data Attributes
– http://blogs.msdn.com/mairaw/archive/2008/04/24/dynamic-data-attributes.aspx