您的位置:首页 > 移动开发

Dynamic Data in Regular Websites/Web Applications

2013-06-09 12:19 309 查看
http://weblogs.asp.net/davidfowler/archive/2009/05/06/dynamic-data-preview-4.aspx


DynamicDatainRegularWebsites/WebApplications

Update:DavidEbbohasagreatvideoonchannel9aboutthis.Youcanwatchithere.

TodayI'mexcitedtosharethatwe'vereleasedDynamicDataPreview4oncodeplex.Checkoutthelatestbitshere.

ThisreleaseisparticularlyinterestingnotonlyforpeoplethathavebeenusingDynamicDataforawhilenow,butanyonethathasanexistingapplicationtodaywhowantstousesomeofthenicetiesDynamicDataofferswithouthavingtotakeallthejunkassociated.
TakealookattheSimpleDynamicDataprojectforexamples.

ExistingSites

Hereare2goodreasonstouseDynamicData:

Richmodelvalidation

RichtemplatingsupportviaFieldTemplates

Ifyou'veeverwrittenadatadrivenappinwebformsusingourdatacontrols,youwouldhaverealizedthatwearelackingalotwhenitcomestovalidation.Youcanenableallofthisgoodnesswithamagicextensionmethod.

protectedvoidPage_Init(){

GridView1.EnableDynamicData(yourtypehere);

}


Note:ThemethodrequiresthatyoupassinthetypethatmayhaveannotationsinorderforustoreadMetadata.Ifyou'renotfamiliarwiththewayannotationsworkinDynamicDatathenwatchthevideoshere.

ThismethodcallenablesDynamicControl/DynamicFieldtoworkwithinanyofthedatacontrolswhichmakesuseofFieldTemplates,
andenablestherichvalidationsupport.

Makingitwork

Soweknowaboutthismagicmethodcallandsomehowcallingitwithatypemakesitalljustwork.Let'swalkthroughanexampleofhowwewouldusethis.Hereismymodel:

publicclassStudent{

[Required]

[Range(0,100)]

publicintAge{get;set;}


[Range(0.0,4.0)]

publicdoubleGPA{get;set;}


[Required]

publicstringFirstName{get;set;}


[Required]

publicstringLastName{get;set;}


[DisplayFormat(DataFormatString="{0:d/M/yyyy}")]

publicDateTimeBirthDate{get;set;}

}


Thisisthetypewearegoingtouseformetadata.UsingtheattributesfromSystem.ComponentModel.DataAnnotationswecanadduseful
annotationstoourmodelthatwillbeusedforvalidationanddisplayformatting.AddingtheseattributesallowsDynamicDatatoenabletheappropriatevalidator.i.eRangeValidator,RequiredFieldValidatoretc.

Nowwe'regoingtoenablethisonourGridViewusingthesamemethodcallasaboveinconjunctionwithanObjectDataSourcetocompleteour
application:

protectedvoidPage_Init(){

GridView1.EnableDynamicData(typeof(Student));

}


Markup

<asp:GridViewID="GridView1"runat="server"DataSourceID="ObjectDataSource1"AutoGenerateEditButton="true">

</asp:GridView>

<asp:ObjectDataSourceID="ObjectDataSource1"runat="server"

DataObjectTypeName="Student"DeleteMethod="DeleteStudent"

InsertMethod="InsertStudent"SelectMethod="GetStudents"

TypeName="StudentsRepository"UpdateMethod="UpdateStudent">

</asp:ObjectDataSource>


Note:DynamicDatatakescareoftheMetadatanotthedata.Youstillneeddatabindthedatacontrolagainstsomedatasource/datasourcecontrol.

Herearetheresultswhenweareediting:





Asyoucansee,theattributeswespecifiedonourStudentclassdirectlyaffectthegridandvalidationisenabled.

There'smorecoolstufftotalkaboutbutI'llmentionthoseinupcomingposts.Fornow,downloadthepreviewandreaduponDynamicData!

PublishedWednesday,May06,20096:33PMbydavidfowl
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐