w3pop.com :: ÍøÂçѧԺ :: ASP.NET :: ASP.NET 2.0 - Master Pages ¿ØÖÆÒ³[Ö÷Ò³]
ASP.NET 2.0 - Master Pages ¿ØÖÆÒ³[Ö÷Ò³]
::
Master pages provide templates for other pages on your web site.
¿ØÖÆÒ³[Ö÷Ò³]¿ÉÒÔÔÚÄãµÄÍøÕ¾ÉÏΪÆäËûÒ³ÃæÌṩģ°å¡£
Master pages allow you to create a consistent look and behavior for all the pages (or group of pages) in your web application.
ÔÚÄãµÄWEB³ÌÐòÖÐʹÓÿØÖÆÒ³¿ÉÒÔΪËùÓÐÒ³Ãæ½¨Á¢Í³Ò»µÄÍâ¹ÛÒÔ¼°ÐÐΪ£¨»òÊÇÒ»×éÒ³Ãæ£©
A master page provides a template for other pages, with shared layout and functionality. The master page defines placeholders for the content, which can be overridden by content pages. The output result is a combination of the master page and the content page.
ͨ¹ý¹²Ïí²¼¾ÖÒÔ¼°¹¦ÄÜ£¬¿ØÖÆÒ³¿ÉÒÔΪÆäËûÒ³ÃæÌṩģ°å¡£¿ØÖÆÒ³¿ÉÒÔΪ±ä¶¯µÄÄÚÈݶ¨Òåռλ·û£¬×îºóÊä³öµÄ½á¹û¾ÍÊÇ¿ØÖÆÒ³ºÍÄÚÈÝÒ³µÄ½áºÏ¡£
The content pages contains the content you want to display.
ÔÚÄÚÈÝÒ³Öаüº¬ÁËÄãÏëÒªÏÔʾµÄÄÚÈÝ
When users request the content page, ASP.NET merges the pages to produce output that combines the layout of the master page with the content of the content page.
µ±Óû§ÇëÇóÄÚÈÝÒ³ÃæµÄʱºò£¬ASP.NET¾Í»áʹÓÃÄÚÈÝÒ³µÄÄÚÈݺͿØÖÆÒ³µÄ²¼¾Ö½øÐнáºÏºóÔٰѽá¹ûÊä³ö¸øÓû§
<%@ Master %> |
The master page above is a normal HTML page designed as a template for other pages.
ÉÏÃæµÄ¿ØÖÆÒ³ÊǸöÆÕͨµÄHTMLÒ³Ãæ¿ÉÒÔΪÆäËûÒ³Ãæ×öΪģ°åʹÓá£
The @ Master directive defines it as a master page.
@ Master ¿ÉÒÔָʾÕâ¸öÎļþ×÷Ϊ¿ØÖÆÒ³
The master page contains a placeholder tag <asp:ContentPlaceHolder> for individual content.
¿ØÖÆÒ³°üº¬ÁËռλ·û±êÇ© <asp:ContentPlaceHolder> ÀïÃæ¿ÉÒÔ¼ÓÈë¸ö±ðµÄÄÚÈÝ¡£
The id="CHP1" attribute identifies the placeholder, allowing many placeholders in the same master page.
ÊôÐÔ id="CHP1" ¶¨ÒåÁËռλ·û£¬ÕâÑù¾Í¿ÉÒÔÔÚͬһ¸ö¿ØÖÆÒ³Àï·ÅÖöà¸öռλ·û¡£
This master page was saved with the name "master1.master"
Õâ¸ö¿ØÖÆÒ³±£´æÎª "master1.master"
Note: The master page can also contain code, allowing dynamic content.
×¢Ò⣺¿ØÖÆÒ³»¹¿ÉÒÔ°üº¬´úÂ룬ÕâÑù¾Í¿ÉÒÔ²úÉú¶¯Ì¬µÄÄÚÈÝ
<%@ Page MasterPageFile="master1.master" %> |
The content page above is one of the individual content pages of the web.
ÉÏÃæµÄÄÚÈÝҳΪWEBÖе¥¶ÀµÄÒ»¸öÄÚÈÝÒ³
The @ Page directive defines it as a standard content page.
@ Page ¾Í±íÃ÷Õâ¸öÎļþÊÇ×÷ΪÄÚÈÝÒ³µÄ¡£
The content page contains a content tag <asp:Content> with a reference to the master page (ContentPlaceHolderId="CPH1").
ÄÚÈÝÒ³°üº¬ÁËÄÚÈݱêÇ© <asp:Content> Õâ¿ÉÒÔ¸ú¿ØÖÆÒ³µÄռλ·û(ContentPlaceHolderId="CPH1")¶ÔÓ¦ÆðÀ´¡£
This content page was saved with the name "mypage1.aspx"
½«Õâ¸öÄÚÈÝÒ³±£´æÎª "mypage1.aspx"
When the user requests this page, ASP.NET merges the content page with the master page.
µ±Óû§ÇëÇóÕâ¸öÒ³ÃæµÄʱºò£¬ASP.NET¾Í½«Õâ¸öÄÚÈÝÒ³¸ú¿ØÖÆÒ³½áºÏÆðÀ´¡£
Click to display mypage1.aspx.[²é¿´ÏÔʾµÄ½á¹û]
Note: The content text must be inside the <asp:Content> tag. No content is allowed outside the tag.
×¢Ò⣺ҪÏÔʾµÄÎÄ×ÖÄÚÈݱØÐëÔÚ<asp:Content>±êÇ©ÀÄÚÈݲ»ÄÜдÔÚ±êÇ©µÄÍâÃæ
<%@ Page MasterPageFile="master1.master" %> |
The content page above demonstrates how .NET controls can be inserted into the content page just like an into an ordinary page.
ÉÏÃæµÄÄÚÈÝÒ³ÑÝʾÁË.NET¿Ø¼þ¿ÉÒÔ²åÈëÄÚÈÝÒ³£¬Õâ¾Í¸ú²åÈëÆÕͨµÄÒ³ÃæÒ»Ñù¡£
Click to display mypage2.aspx.[²é¿´Ð§¹û]
ÆÀÂÛ (0)
All