Transcript
Page 1: ASP.Net 3.5 SP1 Dynamic Data

ASP.Net 3.5 SP1-Dynamic Data

Michał Morciniec – GPSD [email protected]

Page 2: ASP.Net 3.5 SP1 Dynamic Data

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

Page 3: ASP.Net 3.5 SP1 Dynamic Data

Architecture

Data Source Data Model Data ModelMeta-dataapplication

Page generateddynamically

LIN

Q t

oSQ

LPage and Field Templates

Tables, columns,constraints

Entities, Field,Classes, Metadata

Partial classes,Attributes

Page 4: ASP.Net 3.5 SP1 Dynamic Data

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 structureand data relationships.

• Basic data validation rules are imposed automatically

• Pages are generated automatically based ontemplates

• URL Routing feature is used to map URLs to pages.

Page 5: ASP.Net 3.5 SP1 Dynamic Data

Dynamic Data WebSite – Creation 1

• Use template“Dynamic Data Web Site”

• Web Site Project contains “DynamicData” folder

Page 6: ASP.Net 3.5 SP1 Dynamic Data

Dynamic Data WebSite – Creation 2

• Add LINQ to SQL o LINQ to Entityclasses to project

• Note - App_Codefolder is added withthe Data Model(.dbml)

Page 7: ASP.Net 3.5 SP1 Dynamic Data

Dynamic Data WebSite – Creation 3

• Create data connection, dragtables to designer of Data Model

• Data Model (classes ) is generated in “code-behind” associatesdwith the model(.designer.cs)

Page 8: ASP.Net 3.5 SP1 Dynamic Data

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 }

);

Page 9: ASP.Net 3.5 SP1 Dynamic Data

Dynamic Data-Creation DEMO

Page 10: ASP.Net 3.5 SP1 Dynamic Data

Page Templates

• Located in DynamicData\Page Templates

• Page Uses Controls-– DynamicDataManager

– DynamicValidator

– FilterRepeater

– DynamicControl

– DynamicField

• GridView used to show data.

Page 11: ASP.Net 3.5 SP1 Dynamic Data

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 withdifferent tables (entities)

Page 12: ASP.Net 3.5 SP1 Dynamic Data

Separate-Page Mode

• Uses following route in Global.asax– {table}/{action}.

• {action} can be– Edit

– List

– Details

– Insert

• Every operation uses different page– No “in-line” editing

routes.Add(

new DynamicDataRoute("{table}/{action}.aspx")

{

Constraints = new RouteValueDictionary(

new { action = "List|Details|Edit|Insert" }),

Model = model

});

Page 13: ASP.Net 3.5 SP1 Dynamic Data

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("{table}/ListDetails.aspx")

{ Action = PageAction.List,

ViewName = "ListDetails",

Model = model

});

routes.Add(

new DynamicDataRoute("{table}/ListDetails.aspx")

{ Action = PageAction.Details,

ViewName = "ListDetails",

Model = model

});

Page 14: ASP.Net 3.5 SP1 Dynamic Data

Page Modes combination

• Each entity can use its Page Mode:

• Order is significantroutes.Add(

new DynamicDataRoute(“Products/{action}.aspx")

{

Constraints = new RouteValueDictionary(

new { action = "List|Details|Edit|Insert" }),

Model = model,

Table = “Products”

});

routes.Add(

new DynamicDataRoute("{table}/ListDetails.aspx")

{ Action = PageAction.List,

ViewName = "ListDetails",

Model = model

});

routes.Add(

new DynamicDataRoute("{table}/ListDetails.aspx")

{ Action = PageAction.Details,

ViewName = "ListDetails",

Model = model

});

Page 15: ASP.Net 3.5 SP1 Dynamic Data

Field Templates

• Dynamic Data selects controlsbased on field datatype– Boolean shown as checkbox

– Relationships shown as drop down list

– Text show as TextBox or Literal

• Pages use Field Templates toshow control at runtime– Controls do not form part of Page Template

markup, injected at runtime

• FieldTemplates are stored in DynamicData\FieldTemplates– “Intrinsic types”

Page 16: ASP.Net 3.5 SP1 Dynamic Data

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)

Page 17: ASP.Net 3.5 SP1 Dynamic Data

Custom Page Templates

• Page Templates can be designed

• They follow naming convention– DynamicData\CustomPages\{entity_name

}\PageName.aspx

• To associate page templateListDetails.aspxwith tableSuppliers store it in– DynamicData\CustomPages\Supplier

s\ListDetails.aspx

Page 18: ASP.Net 3.5 SP1 Dynamic Data

Custom Page Templates

• Tipically, page markup contains DynamicField control

– Concrete control for field is selected at runtime

• 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

<Columns>

<asp:DynamicField DataField="CompanyName" />

<asp:DynamicField DataField="Phone" />

<asp:DynamicField DataField="Fax" />

<asp:DynamicField DataField="Products" />

</Columns>

Page 19: ASP.Net 3.5 SP1 Dynamic Data

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 FieldTemplateUserControl

• Property FieldValue retrieves field data

<asp:Calendar ID="Calendar1" runat="server“

SelectedDate="<%# (FieldValue!=null)? FieldValue:DateTime.Now %>"

VisibleDate="<%# (FieldValue!=null)? FieldValue:DateTime.Now %>"

</asp:Calendar>

Page 20: ASP.Net 3.5 SP1 Dynamic Data

Custom Field Template-Edit Mode

• Use ExtractValues() to write control data toDB

protected override void ExtractValues(IOrderedDictionary

dictionary) {

dictionary[Column.Name] =

ConvertEditedValue(Calendar1.SelectedDate.ToShortDateString());

}

Page 21: ASP.Net 3.5 SP1 Dynamic Data

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("DateTimeCalendar")]

public object HireDate { get; set; }

}

Page 22: ASP.Net 3.5 SP1 Dynamic Data

Data Validation

• Can use metadata

– [Required()]

– [Range(0,100)]

public class CustomerMetadata {

[Required()]

public object Title;

}

• 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(

"Title must start with an uppercase letter.");

}

}

}

Page 23: ASP.Net 3.5 SP1 Dynamic Data

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”);

}

}

}

Page 24: ASP.Net 3.5 SP1 Dynamic Data

Customization-DEMO

Page 25: ASP.Net 3.5 SP1 Dynamic Data

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.

Page 26: ASP.Net 3.5 SP1 Dynamic Data

Dynamic Data Wizard

Page 27: ASP.Net 3.5 SP1 Dynamic Data

Step1: Data Model

• Data Model

– New Data Model generation

– Can select existing model

Page 28: ASP.Net 3.5 SP1 Dynamic Data

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

Page 29: ASP.Net 3.5 SP1 Dynamic Data

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)

Page 30: ASP.Net 3.5 SP1 Dynamic Data

Step4: Operation Customization

• Select/Restrict operations type

– edition, insertion, deletion, details

• You can select fields for each view

Page 31: ASP.Net 3.5 SP1 Dynamic Data

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

Page 32: ASP.Net 3.5 SP1 Dynamic Data

Dynamic Data Wizard - DEMO

Page 33: ASP.Net 3.5 SP1 Dynamic Data

Summary

• Dynamic Data facilitates creation of data-driven web sites

– Increases development productivity

– Simplifies maintenance

– Possibility of customization

• Dynamic Data Wizard still under development

Page 34: ASP.Net 3.5 SP1 Dynamic Data

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


Recommended