|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
ASP由于使用了COM组件所以它会变的十分强大,但是这样的强大由于WindowsNT系统最初的设计问题而会引发大量的安全问题。只要在这样的组件或是操作中一不注意,哪么外部攻击就可以取得相当高的权限而导致网站瘫痪或者数据丢失;asp.net|asp.netbegin:
MasteringASP.NetDataBinding
KarlSeguin?karlseguin@hotmail.com
TableofContents
Introduction
TheSampleProgram
UnderstandingDataItem
Formatting
Inline
OnItemDataBound
OnItemCreated
NestedBinding
Inline
OnItemDataBound
HandlingEvents
Download
ThisarticleisavailableatCodeProject.Checkitouttomakecomments,discussorratethearticle
IdliketothankJean-ClaudeManolifordevelopinghisC#Codeformat,whichiusedinwritingthistutorial.
Introduction
Questionsregardingdatabinding,inoneformoranother,areprobablythemostaskedintheaspnetnewsgroups.Itscleareveryonelovestheideaofdatabindingbutthatmoreadvancedfunctionality,suchaseventhandling,conditionalformattingandfine-tuning,arentstraightforward.Thegoalofthistutorialisshedlightonsomeofthemorecommonandfrequentlyaskedquestionsaboutthecapabilitiesofdatabinding.
TheSampleProgram
Throughoutthistutorialwellusetwoseparatedatasources.Thefirstwillbeyourevery-daydataset,theotherwillbeastrongly-typedcustomcollectioncontainingstrongly-typedobjects.
Ourdatasetwillcontaintwotables,CustomersandOrders:
CustomerStructureOrderStructure
NameTypeDescriptionNameTypeDescription
CustomerId1Int32UniquecustomeridentifierOrderIdInt32Uniqueorderidentifier
NameStringNameofthecustomerCustomerId1Int32Identifierofthecustomwhoplacedtheorder
ZipStringCustomersprimaryZIPorPortalcodeOrderedDateTimeDatetheorderwasplacedon
EnabledBooleanWhetherthecustomeriscurrentlyactive/enabledAmountDecimalDollarvalueoftheorder
1ADataRelationexistsbetweentheCustomer.CustomerIdandOrder.CustomerIdcolumns.
OurbusinessentitieswillconsistofanOwnerandaPetclass:
OwnerStructurePetsStructure
NameTypeDescriptionNameTypeDescription
OwnerIdInt32UniqueowneridentifierPetIdInt32Uniquepetidentifier
YearOfBirthInt32TheyeartheownerwasborninNameStringNameofthepet
FirstNameStringOwnersfirstnameIsNeuturedBooleanWhetherornotthepetisneutured
LastNameStringOwnerslastnameTypePetTypeIndicatesthetypeofpet(Dog,Cat,Fish,Bird,Rodent,Other)
PetsPetCollectionCollectionofpetstheownerhas
UnderstandingDataItem
YouveundoudbtedlymadefrequentuseoftheDataItemproperty,namelywhenusingtheDataBindingsyntaxtooutputavalue:
1:<%#DataBinder.Eval(Container.DataItem,"customerId")%>
ItsimportanttounderstandthatDataItemisactuallyanobject,andthatwhenyouusetheDataBinder.Evalfunction,itbasicallyneedstofigureoutwhattypeofobjectitisandhowtoget"customerId"fromit.ThatsbecauseyourDataSourcecanbedifferentthings,suchasadatasetordataview,anarraylistorhashtable,acustomcollectionandmore.Bindinghappensonarow-by-rowbasisandDataItemactuallyrepresentsthecurrentrowbeingbound.ForaDataSet,DataTableorDataViewDataItemisactuallyaninstanceofDataRowView(youmightthinkthattheDataItemforaDataSetorDataTablewouldbeaninstanceofDataRow,butwhenyoubindeitherofthese,theDefaultViewisactuallyused,thereforeDataItemwillalwaysbeaDataRowView).Whenyouarebindingtoacollection,DataItemisaninstanceoftheitemwithinthecollection.Wecanobservethismoreclearlywiththefollowingcode:
1:<%@Importnamespace="System.Data"%>
2:<%@Importnamespace="BindingSample"%>
3:<asp:Repeaterid="dataSetRepeater"Runat="server">
4:<ItemTemplate>
5:<%#((DataRowView)Container.DataItem)["customerId"]%>-
6:<%#((DataRowView)Container.DataItem)["Name"]%><br/>
7:</ItemTemplate>
8:<AlternatingItemTemplate>
9:<%#DataBinder.Eval(Container.DataItem,"customerId")%>-
10:<%#DataBinder.Eval(Container.DataItem,"Name")%><br/>
11:</AlternatingItemTemplate>
12:</asp:Repeater>
13:
14:<br><br>
15:
16:<asp:Repeaterid="collectionRepeater"Runat="server">
17:<ItemTemplate>
18:<%#((Owner)Container.DataItem).OwnerId%>-
19:<%#((Owner)Container.DataItem).FirstName%><</p>缺点:安全性不是太差了,还行,只要你充分利用系统自带的工具;唯一缺点就是执行效率慢,如何进行网站优化以后,效果会比较好。 |
|