Tải bản đầy đủ - 0 (trang)
Chapter 10. Creating and Using Master Pages

Chapter 10. Creating and Using Master Pages

Tải bản đầy đủ - 0trang

Chapter10.CreatingandUsingMaster

Pages

INTHISCHAPTER

AnIntroductiontoMasterPages144

StructureofMasterPagesandContentPages145

CreatingaMasterPage147

ConfiguringNavigationControls155

ConnectingaPagetotheMasterPage159

Checkpoint161



AnIntroductiontoMasterPages

WhenMicrosoftdecidedtostopdevelopingFrontPage,Igot

manyemailsfromwebdesignersaskingmehowtobestgo

aboutconvertingawebsitefromFrontPagetoExpressionWeb.

Myanswerwasalwaysthesame:Don'tworryaboutconverting

yoursite.Instead,usethisasanopportunitytoredesignyour

siteandgiveitafreshappearance.Thatrecommendationwas

alwaysmetwithresistancebecauseredesigningawebsiteisa

dauntingtask.However,ifyoudesignyoursitetobeeasily

updatedandmaintained,it'snotnearlyashardasyoumight

think.

ASP.NETmasterpagescanmakeiteasytocreateandmaintain

awebapplicationwithaconsistentlookandfeel.Amasterpage

isaspecialkindofASP.NETpagethatisusedsomewhatlikea

templateforotherASP.NETpages.WhenanotherASP.NETpage

usesanASP.NETmasterpage,ittakesonallthequalitiesofthe

masterpage,includingtheappearanceandanycodeassociated

withthemasterpage.However,youcandefineareasinthe

masterpagethatapageusingthatmasterpagecanoverride

withitsowncontentbyusingContentPlaceHoldercontrols.



EachtimeyoucreateanewASP.NETWebform,youhavethe

optionofselectingamasterpagetousewithit.Ifyouselecta

masterpage,thenewWebformimmediatelytakesonthe

appearanceofthemasterpageandisreferredtoasacontent

pageforthemasterpage.Ifanareaofthatmasterpage

containsaContentPlaceHoldercontrol,youhavetheoptionof

eitherusingthecontentthatthemasterpageprovidesforthe

ContentPlaceHoldercontrolorofcreatingcontentforthe

ContentPlaceHoldercontrolthatisuniquetothecontentpage.

MostASP.NETdevelopersusemasterpagestodefineacommon

userinterfacefortheirwebapplications.Contentthatremains

thesameonallpagesiscontainedwithinthemasterpageand

outsideanyContentPlaceHoldercontrols.However,anyareaof

thepagethatshouldcontainpage-specificdataincludesa

ContentPlaceHoldercontrol.Figure10.1illustratesthisconcept.

Figure10.1.AtypicalASP.NETmasterpageshowinga

singleContentPlaceHoldercontrol.

[Viewfullsizeimage]



ThemasterpageshowninFigure10.1containsnavigational

controls,alogo,andcopyrighttextthatwillbeincludedonall

pagesthatusethemasterpage.Italsocontainsa

ContentPlaceHoldercontrolnamedmainContent.Whena

contentpageusesthismasterpage,onlythemainContent

sectionofthepageiseditable.Therestofthepageislocked

andcontentisprovidedbythemasterpage.You'llhavean

opportunitytoseethisconceptinactionlaterinthischapter.



PartIII:MasterPagesandUserControls







10CreatingandUsingMasterPages

11CreatingandUsingUserControls



Chapter10.CreatingandUsingMaster

Pages

INTHISCHAPTER

AnIntroductiontoMasterPages144

StructureofMasterPagesandContentPages145

CreatingaMasterPage147

ConfiguringNavigationControls155

ConnectingaPagetotheMasterPage159

Checkpoint161



AnIntroductiontoMasterPages

WhenMicrosoftdecidedtostopdevelopingFrontPage,Igot

manyemailsfromwebdesignersaskingmehowtobestgo

aboutconvertingawebsitefromFrontPagetoExpressionWeb.

Myanswerwasalwaysthesame:Don'tworryaboutconverting

yoursite.Instead,usethisasanopportunitytoredesignyour

siteandgiveitafreshappearance.Thatrecommendationwas

alwaysmetwithresistancebecauseredesigningawebsiteisa

dauntingtask.However,ifyoudesignyoursitetobeeasily

updatedandmaintained,it'snotnearlyashardasyoumight

think.

ASP.NETmasterpagescanmakeiteasytocreateandmaintain

awebapplicationwithaconsistentlookandfeel.Amasterpage

isaspecialkindofASP.NETpagethatisusedsomewhatlikea

templateforotherASP.NETpages.WhenanotherASP.NETpage

usesanASP.NETmasterpage,ittakesonallthequalitiesofthe

masterpage,includingtheappearanceandanycodeassociated

withthemasterpage.However,youcandefineareasinthe

masterpagethatapageusingthatmasterpagecanoverride

withitsowncontentbyusingContentPlaceHoldercontrols.



EachtimeyoucreateanewASP.NETWebform,youhavethe

optionofselectingamasterpagetousewithit.Ifyouselecta

masterpage,thenewWebformimmediatelytakesonthe

appearanceofthemasterpageandisreferredtoasacontent

pageforthemasterpage.Ifanareaofthatmasterpage

containsaContentPlaceHoldercontrol,youhavetheoptionof

eitherusingthecontentthatthemasterpageprovidesforthe

ContentPlaceHoldercontrolorofcreatingcontentforthe

ContentPlaceHoldercontrolthatisuniquetothecontentpage.

MostASP.NETdevelopersusemasterpagestodefineacommon

userinterfacefortheirwebapplications.Contentthatremains

thesameonallpagesiscontainedwithinthemasterpageand

outsideanyContentPlaceHoldercontrols.However,anyareaof

thepagethatshouldcontainpage-specificdataincludesa

ContentPlaceHoldercontrol.Figure10.1illustratesthisconcept.

Figure10.1.AtypicalASP.NETmasterpageshowinga

singleContentPlaceHoldercontrol.

[Viewfullsizeimage]



ThemasterpageshowninFigure10.1containsnavigational

controls,alogo,andcopyrighttextthatwillbeincludedonall

pagesthatusethemasterpage.Italsocontainsa

ContentPlaceHoldercontrolnamedmainContent.Whena

contentpageusesthismasterpage,onlythemainContent

sectionofthepageiseditable.Therestofthepageislocked

andcontentisprovidedbythemasterpage.You'llhavean

opportunitytoseethisconceptinactionlaterinthischapter.



StructureofMasterPagesandContent

Pages

Aspreviouslymentioned,amasterpageconsistsofregular

pagecontentalongwithoneormoreContentPlaceHolder

controls.UnlikearegularASP.NETWebform,amasterpage

usesa@Masterdirectiveatthetopofthepage.Thefollowing

codeshowsthe@Masterdirectivefromthemasterpageshown

previouslyinFigure10.1.

CodeView:



<%@MasterLanguage="C#"AutoEventWireup="true"CodeFile="Maste

























FormoreinformationonASP.NETdirectives,see

"ASP.NETDirectives,"p.22.



Otherthanthedifferentdirectiveusedtodefinethepage,

masterpagesareverymuchlikeanyotherASP.NETWebform.

Contentpages,ontheotherhand,arequitedifferentfrom

otherASP.NETpages.Becauseacontentpagegetsalargepart

ofitscontentfromthemasterpage,itdoesnotcontainthe

traditionalHTMLtagsthatyou'reusedtoseeing.Thecodein

Listing10.1containsallthecodevisibleinSourceviewaftera

masterpageisusedtocreateaWebformasacontentpage.

Listing10.1.ContentPageCode

CodeView:



<%@PageLanguage="C#"MasterPageFile="~/MasterPage.master"Aut

































Thefirstthingthatyou'llnoticeistheMasterPageFileattribute

inthe@Pagedirective.Thisattributeconfigurestheconnection

betweenthecontentpageandthemasterpage.Theonlyother

codeinthepageconsistsoftwoContentcontrols(indicatedby

thetags)calledContent1andContent2.

ContentcontrolsareASP.NETcontrolsthatcorrespondto

specificContentPlaceHoldercontrolsonthemasterpage.Each

ContentcontrolhasaContentPlaceHolderIDattributethat

specifiestheContentPlaceHoldercontrolonthemasterpage

thatcorrespondstotheContentcontrol.Forexample,anything

thatisenteredintotheContent2ContentcontrolinListing10.1

willappearinsidetheareaindicatedbythemainContent

ContentPlaceHoldercontrolshownpreviouslyinFigure10.1.

Tip

RememberthatContentPlaceHoldercontrolsbelongon

masterpagesandContentcontrolsbelongoncontent

pages.



Amasterpagecanalsoprovidecontentitselfforaparticular

ContentPlaceHoldercontrol.Forexample,youmighthavea

ContentPlaceHoldercontrolthatcontainsatelephonenumber

foryouroffice,butyouhavesomepagesthatrefertoa

differentofficewithadifferentphonenumber.Ifyouenterthe

telephonenumberintoaContentPlaceHoldercontrol,anypage

thatusesthemasterpagewillautomaticallyshowthe

telephonenumberyouentered,asshowninFigure10.2.



Figure10.2.ThephoneContentContentPlaceHoldercontrol

containsaphonenumberthatwasenteredintothe

controlonthemasterpage.

[Viewfullsizeimage]



Onthepageswhereyouneedtospecifyadifferentphone

number,simplyclicktheSmartTagbuttonasshowninFigure

10.3andselectCreateCustomContent.Whenyoudo,the

contentfromthemasterpageisreplacedwithemptycontentso

thatyoucanprovideyourown.

Figure10.3.AContentcontrolcanprovideitsownunique

contentbyusingtheSmartTagfortheContentcontrol.

[Viewfullsizeimage]



Ifyou'dliketorevertbacktothecontentfromthemasterpage,

clicktheSmartTagbuttonagainandselectDefaulttoMaster's

Content.Whenyoudo,anycontentthatyou'veaddedtothe

Contentcontrolisremovedandreplacedwiththecontentfrom

themasterpage.

Caution

Ifyouswitchtothemasterpage'scontent,any

informationthatexistswithintheContentcontrolis

deleted.Youcanundothatoperation,butonlyifyou

haven'talreadymadeotheredits.Keepinmindthat

changingtheContentcontrolbacktousingcustom

contentdoesnotrestorethepreviouscontent.



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Chapter 10. Creating and Using Master Pages

Tải bản đầy đủ ngay(0 tr)

×