|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
ASP在国内异常流行,因为国内大多使用的是盗版的Windows和盗版的SQLServer,而ASP+COM+SQLServer实际上也是一种不错的搭配,其性能也不输于PHP+MYSQL,特别是Windows系统和SQLServer都有图形界面,比APACHE和MYSQL易于维护,因此对于不重视知识产权的国家来说也是一种不错的选择。OneofthethingsthatIpersonallyfoundfrustratingwithclassicASPwasthedifficultyassociatedwithcompletingmanycommonWeb-relatedtasks.Forexample,theneedtoallowWebvisitorstouploadfilestotheWebserverisfairlycommonforWebdevelopers;however,withclassicASPtheonlywaytoaccomplishthiswithoutmuchdifficultywasthroughtheuseofathird-partyCOMcomponent.Similarly,commontaskssuchassendingemails,readingandwritingtotheWindowsEventLog,workingwiththeWebserversfilesystem,anddynamicallygeneratingimagesbasedondatabaseinformationwerealltricky,ifnotimpossible,withouttheaidofaCOMcomponent.
ThankfullythishasallchangedwithASP.NET.NowWebdeveloperscaneasilyaccomplishaplethoraofcommontaskswithouttheneedtocreateorbuyathird-partycomponentthanksinlargeparttoASP.NETbeingpartoftherobust.NETFramework.WhenyouinstalltheASP.NETsoftwareonyourcomputerfromtheCDaccompanyingthisbook,inadditiontotheASP.NETengine,theentire.NETFrameworkwillbeinstalled.The.NETFrameworkconsistsofhundredsofclassesbrokendownintoanumberoflogicalnamespaces.TheseclassesprovidethemethodsandpropertiesneededtocreatepowerfulWindowsapplications,fromstandalonedesktopappstoInternetapplications.
ASP.NETWebpagescanutilizeanyofthesehundredsofclasses,givingASP.NETWebpagesthepowerandflexibilitythatclassicASPdeveloperscouldonlyreceivewiththeuseofbulkyCOMcomponents.InthischapterwewillexaminemanyofthenewfeaturesthatweredifficulttoimplementwithclassicASPbutcanbeeasilyperformedwithanASP.NETWebpage.
1.UsingCollections
Mostmodernprogramminglanguagesprovidesupportforsometypeofobjectthatcanholdavariablenumberofelements.Theseobjectsarereferredtoascollections,andtheycanhaveelementsaddedandremovedwitheasewithouthavingtoworryaboutpropermemoryallocation.IfyouveprogrammedwithclassicASPbefore,youreprobablyfamiliarwiththeScripting.Dictionaryobject,acollectionobjectthatreferenceseachelementwithatextualkey.Acollectionthatstoresobjectsinthisfashionisknownasahashtable.
Therearemanytypesofcollectionsinadditiontothehashtable.Eachtypeofcollectionissimilarinpurpose:Itservesasameanstostoreavaryingnumberofelements,providinganeasyway,ataminimum,toaddandremoveelements.Eachdifferenttypeofcollectionisuniqueinitsmethodofstoring,retrieving,andreferencingitsvariouselements.
The.NETFrameworkprovidesanumberofcollectiontypesforthedevelopertouse.Infact,anentirenamespace,System.Collections,isdedicatedtocollectiontypesandhelperclasses.EachofthesecollectiontypescanstoreelementsoftypeObject.Becausein.NETallprimitivedatatypes―string,integers,date/times,arrays,andsoon―arederivedfromtheObjectclass,thesecollectionscanliterallystoreanything!Forexample,youcoulduseasinglecollectiontostoreacoupleofintegers,aninstanceofaclassicCOMcomponent,astring,adate/time,andtwoinstancesofacustom-written.NETcomponent.Mostoftheexamplesinthissectionusecollectionstohouseprimitivedatatypes(strings,integers,doubles).However,Listing2.1.8(whichappearsinthe"SimilaritiesAmongtheCollectionTypes"section)illustratesacollectionofcollections―thatis,acollectiontypethatstoresentirecollectionsaseachofitselements!
Throughoutthissectionwellexaminefivecollectionsthe.NETFrameworkoffersdevelopers:theArrayList,theHashtable,theSortedList,theQueue,andtheStack.Asyoustudyeachofthesecollections,realizethattheyallhavemanysimilarities.Forexample,eachtypeofcollectioncanbeiteratedthroughelement-by-elementusingaForEach...NextloopinVB(oraforeachloopinC#).Eachcollectiontypehasanumberofsimilarlynamedfunctionsthatperformthesametasks.Forexample,eachcollectiontypehasaClearmethodthatremovesallelementsfromthecollection,andaCountpropertythatreturnsthenumberofelementsinthecollection.Infact,thelastsubsection"SimilaritiesAmongtheCollectionTypes"examinesthecommontraitsfoundamongthecollectiontypes.
WorkingwiththeArrayListClass
ThefirsttypeofcollectionwelllookatistheArrayList.WithanArrayList,eachitemisstoredinsequentialorderandisindexednumerically.Inourfollowingexamples,keepinmindthatthedeveloperneednotworryhimselfwithmemoryallocation.Withthestandardarray,thedevelopercannoteasilyaddandremoveelementswithoutconcerninghimselfwiththesizeandmakeupofthearray.Withallthecollectionswellexamineinthischapter,thisisnolongeraconcern.
Adding,Removing,andIndexingElementsinanArrayList
TheArrayListclasscontainsanumberofmethodsforaddingandremovingObjectsfromthecollection.TheseincludeAdd,AddRange,Insert,Remove,RemoveAt,RemoveRange,andClear,allofwhichwellexamineinListing2.1.1.TheoutputisshowninFigure2.1.
Listing2.1.1ForSequentiallyAccessedCollections,UsetheArrayList
1:<scriptlanguage="vb"runat="server">
2:
3:SubPage_Load(senderasObject,easEventArgs)
4:CreatetwoArrayLists,aTerritoriesandaStates
5:DimaTerritoriesasNewArrayList
6:DimaStatesasNewArrayList
7:
8:UsetheAddmethodtoaddthe50statesoftheUS
9:aStates.Add("Alabama")
10:aStates.Add("Alaska")
11:aStates.Add("Arkansas")
12:...
13:aStates.Add("Wyoming")
14:
15:Buildupourlistofterritories,whichincludes
16:all50statesplussomeadditionalcountries
17:aTerritories.AddRange(aStates)addall50states
18:aTerritories.Add("Guam")
19:aTerritories.Add("PuertoRico")
20:
21:WedlikethefirstterritorytobetheDistrictofColumbia,
22:sowellexplicitlyaddittothebeginningoftheArrayList
23:aTerritories.Insert(0,"DistrictofColumbia")
24:
25:Displayalloftheterritorieswithaforloop
26:lblTerritories.Text="<i>Thereare"&aTerritories.Count&_
27:"territories...</i><br>"
28:
29:DimiasInteger
30:Fori=0toaTerritories.Count-1
31:lblTerritories.Text=lblTerritories.Text&_
32:aTerritories(i)&"<br>"
33:Next
34:
35:Wecanremoveobjectsinoneoffourways:
36:...Wecanremoveaspecificitem
37:aTerritories.Remove("Wyoming")
38:
39:...Wecanremoveanelementataspecificposition
40:aTerritories.RemoveAt(0)willgetridofDistrict
41:ofColumbia,
42:thefirstelement
43:
44:Displayalloftheterritorieswithforeachloop
45:lblFewerTerritories.Text="<i>Therearenow"&_
46:aTerritories.Count&"territories...</i><br>"
47:
48:DimsasString
49:ForEachsinaTerritories
50:lblFewerTerritories.Text=lblFewerTerritories.Text&_
51:s&"<br>"
52:Next
53:
54:...wecanremoveachunkofelementsfromthe
55:arraywithRemoveRange
56:aTerritories.RemoveRange(0,2)willgetridofthe
57:firsttwoelements
58:
59:Displayalloftheterritorieswithforeachloop
60:lblEvenFewerTerritories.Text="<i>Therearenow"&_
61:aTerritories.Count&"territories...</i><br>"
62:
63:ForEachsinaTerritories
64:lblEvenFewerTerritories.Text=lblEvenFewerTerritories.Text&_
65:s&"<br>"
66:Next
67:
68:Finally,wecancleartheENTIREarrayusingtheclearmethod
69:aTerritories.Clear()
70:EndSub
71:
72:</script>
73:
74:<html>
75:<body>
76:<b>TheTerritoriesoftheUnitedStates:</b><br>
77:<asp:labelid="lblTerritories"runat="server"/>
78:
79:<p>
80:
81:<b>AftersomeworkingwiththeTerritoriesArrayList:</b><br>
82:<asp:labelid="lblFewerTerritories"runat="server"/>
83:
84:<p>
85:
86:<b>AfterfurtherworkingwiththeTerritoriesArrayList:</b><br>
87:<asp:labelid="lblEvenFewerTerritories"runat="server"/>
88:</body>
89:</htm
强大的可扩展性。ASP具有强大的扩展性,可以实现与多种网络、硬件设备的连接:通过专用的通讯线路远程接入企业;通过远程拨号服务器为远程拨号客户提供服务;通过WAP为移动电话互联网客户服务。 |
|