外文翻译--ASP.NET技术.doc
英文原文ASP.NETTechnique1.BuildingASP.NETPagesASP.NETandthe.NETFrameworkASP.NETispartofMicrosoftsoverall.NETframework,whichcontainsavastsetofprogrammingclassesdesignedtosatisfyanyconceivableprogrammingneed.Inthefollowingtwosections,youlearnhowASP.NETfitswithinthe.NETframework,andyoulearnaboutthelanguagesyoucanuseinyourASP.NETpages.The.NETFrameworkClassLibraryaginethatyouareMicrosoft.ImaginethatyouhavetosupportmultipleprogramminglanguagessuchasVisualBasic,JScript,andC+.Agreatdealofthefunctionalityoftheseprogramminglanguagesoverlaps.Forexample,foreachlanguage,youwouldhavetoincludemethodsforaccessingthefilesystem,workingwithdatabases,andmanipulatingstrings.Furthermore,theselanguagescontainsimilarprogrammingconstructs.Everylanguage,forexample,canrepresentloopsandconditionals.EventhoughthesyntaxofaconditionalwritteninVisualBasicdiffersfromthesyntaxofaconditionalwritteninC+,theprogrammingfunctionisthesame.Finally,mostprogramminglanguageshavesimilarvariabledatatypes.Inmostlanguages,youhavesomemeansofrepresentingstringsandintegers,forexample.Themaximumandminimumsizeofanintegermightdependonthelanguage,butthebasicdatatypeisthesame.Maintainingallthisfunctionalityformultiplelanguagesrequiresalotofwork.Whykeepreinventingthewheel?Wouldntitbeeasiertocreateallthisfunctionalityonceanduseitforeverylanguage?The.NETFrameworkClassLibrarydoesexactlythat.Itconsistsofavastsetofclassesdesignedtosatisfyanyconceivableprogrammingneed.Forexample,the.NETframeworkcontainsclassesforhandlingdatabaseaccess,workingwiththefilesystem,manipulatingtext,andgeneratinggraphics.Inaddition,itcontainsmorespecializedclassesforperformingtaskssuchasworkingwithregularexpressionsandhandlingnetworkprotocols.The.NETframework,furthermore,containsclassesthatrepresentallthebasicvariabledatatypessuchasstrings,integers,bytes,characters,andarrays.Mostimportantly,forpurposesofthisbook,the.NETFrameworkClassLibrarycontainsclassesforbuildingASP.NETpages.Youneedtounderstand,however,thatyoucanaccessanyofthe.NETframeworkclasseswhenyouarebuildingyourASP.NETpages.UnderstandingNamespacesAsyoumightguess,the.NETframeworkishuge.Itcontainsthousandsofclasses(over3,400).Fortunately,theclassesarenotsimplyjumbledtogether.Theclassesofthe.NETframeworkareorganizedintoahierarchyofnamespaces.ASPClassicNoteInpreviousversionsofActiveServerPages,youhadaccesstoonlyfivestandardclasses(theResponse,Request,Session,Application,andServerobjects).ASP.NET,incontrast,providesyouwithaccesstoover3,400classes!Anamespaceisalogicalgroupingofclasses.Forexample,alltheclassesthatrelatetoworkingwiththefilesystemaregatheredtogetherintotheSystem.IOnamespace.Thenamespacesareorganizedintoahierarchy(alogicaltree).AttherootofthetreeistheSystemnamespace.Thisnamespacecontainsalltheclassesforthebasedatatypes,suchasstringsandarrays.Italsocontainsclassesforworkingwithrandomnumbersanddatesandtimes.Youcanuniquelyidentifyanyclassinthe.NETframeworkbyusingthefullnamespaceoftheclass.Forexample,touniquelyrefertotheclassthatrepresentsafilesystemfile(theFileclass),youwouldusethefollowing:System.IO.FileSystem.IOreferstothenamespace,andFilereferstotheparticularclass.NOTEYoucanviewallthenamespacesofthestandardclassesinthe.NETFrameworkClassLibrarybyviewingtheReferenceDocumentationforthe.NETFramework.StandardASP.NETNamespacesTheclassescontainedinaselectnumberofnamespacesareavailableinyourASP.NETpagesbydefault.(Youmustexplicitlyimportothernamespaces.)ThesedefaultnamespacescontainclassesthatyouusemostofteninyourASP.NETapplications:SystemContainsallthebasedatatypesandotherusefulclassessuchasthoserelatedtogeneratingrandomnumbersandworkingwithdatesandtimes.System.CollectionsContainsclassesforworkingwithstandardcollectiontypessuchashashtables,andarraylists.System.Collections.SpecializedContainsclassesthatrepresentspecializedcollectionssuchaslinkedlistsandstringcollections.System.ConfigurationContainsclassesforworkingwithconfigurationfiles(Web.configfiles).System.TextContainsclassesforencoding,decoding,andmanipulatingthecontentsofstrings.System.Text.RegularExpressionsContainsclassesforperformingregularexpressionmatchandreplaceoperations.System.WebContainsthebasicclassesforworkingwiththeWorldWideWeb,includingclassesforrepresentingbrowserrequestsandserverresponses.System.Web.CachingContainsclassesusedforcachingthecontentofpagesandclassesforperformingcustomcachingoperations.System.Web.SecurityContainsclassesforimplementingauthenticationandauthorizationsuchasFormsandPassportauthentication.System.Web.SessionStateContainsclassesforimplementingsessionstate.System.Web.UIContainsthebasicclassesusedinbuildingtheuserinterfaceofASP.NETpages.System.Web.UI.HTMLControlsContainstheclassesfortheHTMLcontrols.System.Web.UI.WebControlsContainstheclassesfortheWebcontrols.NETFramework-CompatibleLanguagesForpurposesofthisbook,youwillwritetheapplicationlogicforyourASP.NETpagesusingVisualBasicasyourprogramminglanguage.ItisthedefaultlanguageforASP.NETpages.AlthoughyousticktoVisualBasicinthisbook,youalsoneedtounderstandthatyoucancreateASP.NETpagesbyusinganylanguagethatsupportsthe.NETCommonLanguageRuntime.Outofthebox,thisincludesC#,JScript.NET,andtheManagedExtensionstoC+.NOTEDozensofotherlanguagescreatedbycompaniesotherthanMicrosofthavebeendevelopedtoworkwiththe.NETframework.SomeexamplesoftheseotherlanguagesincludePython,SmallTalk,Eiffel,andCOBOL.Thismeansthatyoucould,ifyoureallywantedto,writeASP.NETpagesusingCOBOL.RegardlessofthelanguagethatyouusetodevelopyourASP.NETpages,youneedtounderstandthatASP.NETpagesarecompiledbeforetheyareexecuted.ThismeansthatASP.NETpagescanexecuteveryquickly.ThefirsttimeyourequestanASP.NETpage,thepageiscompiledintoa.NETclass,andtheresultingclassfileissavedbeneathaspecialdirectoryonyourservernamedTemporaryASP.NETFiles.ForeachandeveryASP.NETpage,acorrespondingclassfileappearsintheTemporaryASP.NETFilesdirectory.WheneveryourequestthesameASP.NETpageinthefuture,thecorrespondingclassfileisexecuted.WhenanASP.NETpageiscompiled,itisnotcompileddirectlyintomachinecode.Instead,itiscompiledintoanintermediate-levellanguagecalledMicrosoftIntermediateLanguage(MSIL).All.NET-compatiblelanguagesarecompiledintothisintermediatelanguage.AnASP.NETpageisntcompiledintonativemachinecodeuntilitisactuallyrequestedbyabrowser.Atthatpoint,theclassfilecontainedintheTemporaryASP.NETFilesdirectoryiscompiledwiththe.NETframeworkJustinTime(JIT)compilerandexecuted.Themagicalaspectofthiswholeprocessisthatithappensautomaticallyinthebackground.AllyouhavetodoiscreateatextfilewiththesourcecodeforyourASP.NETpage,andthe.NETframeworkhandlesallthehardworkofconvertingitintocompiledcodeforyou.ASPCLASSICNOTEWhataboutVBScript?BeforeASP.NET,VBScriptwasthemostpopularlanguagefordevelopingActiveServerPages.ASP.NETdoesnotsupportVBScript,andthisisgoodnews.VisualBasicisasupersetofVBScript,whichmeansthatVisualBasichasallthefunctionalityofVBScriptandmore.So,youhavearichersetoffunctionsandstatementswithVisualBasic.Furthermore,unlikeVBScript,VisualBasicisacompiledlanguage.ThismeansthatifyouuseVisualBasictorewritethesamecodethatyouwrotewithVBScript,youcangetbetterperformance.IfyouhaveworkedonlywithVBScriptandnotVisualBasicinthepast,dontworry.SinceVBScriptissocloselyrelatedtoVisualBasic,youllfinditeasytomakethetransitionbetweenthetwolanguages.NOTEMicrosoftincludesaninterestingtoolnamedtheILDisassembler(ILDASM)withthe.NETframework.YoucanusethistooltoviewthedisassembledcodeforanyoftheASP.NETclassesintheTemporaryASP.NETFilesdirectory.Itlistsallthemethodsandpropertiesoftheclassandenablesyoutoviewtheintermediate-levelcode.ThistoolalsoworkswithalltheASP.NETcontrolsdiscussedinthischapter.Forexample,youcanusetheILDisassemblertoviewtheintermediate-levelcodefortheTextBoxcontrol(locatedinafilenamedSystem.Web.dll).IntroducingASP.NETControlsASP.NETcontrolsprovidethedynamicandinteractiveportionsoftheuserinterfaceforyourWebapplication.ThecontrolsrenderthecontentthattheusersofyourWebsiteactuallyseeandinteractwith.Forexample,youcanusecontrolstocreateHTMLformelements,interactivecalendars,androtatingbanneradvertisements.ASP.NETcontrolscoexistpeacefullywithHTMLcontent.Typically,youcreatethestaticareasofyourWebpageswithnormalHTMLcontentandcreatethedynamicorinteractiveportionswithASP.NETcontrols.ThebestwaytounderstandhowASP.NETcontrolsworkinanHTMLpageistolookatasimpleWebFormsPage.AddingApplicationLogictoanASP.NETPageThesecondbuildingblockofanASP.NETpageistheapplicationlogic,whichistheactualprogrammingcodeinthepage.Youaddapplicationlogictoapagetohandlebothcontrolandpageevents.IfauserclicksaButtoncontrolwithinanHTMLform,forexample,theButtoncontrolraisesanevent(theClickevent).Typically,youwanttoaddcodetothepagethatdoessomethinginresponsetothisevent.Forexample,whensomeoneclickstheButtoncontrol,youmightwanttosavetheformdatatoafileordatabase.