ÍøÂçѧԺ w3popÉçÇø ÍøÂç×ÊÔ´ ITÐÂÎÅ

w3pop.com :: ÍøÂçѧԺ :: ASP.NET :: ASP.NET 2.0 - Navigation µ¼º½

»áÔ±µÇ½

ÕʺÅ

ÃÜÂë

»Ø´ð

¼ÇסÃÜÂë

Íü¼ÇÃÜÂë? ×¢²á

ASP.NET
asp.netµÄ°²×°
ASP ºÍ ASP.NET Ö..
ASP.NET ½éÉÜ

ASP.NET 2.0 - Navigation µ¼º½


×÷Õß:w3schools ·­Òë/ÕûÀí:w3pop.com ·¢²¼:2007-08-16 ä¯ÀÀ:4029 :: ::

ASP.NET 2.0 has built-in navigation controls
ÔÚASP.NET2.0ÖÐÄÚÖÃÁ˵¼º½¿Ø¼þ


Web Site Navigation
ÍøÕ¾µ¼º½

Maintaining the menu of a large web site is difficult and time consuming.
ҪΪ´óÐÍÍøÕ¾Î¬»¤²Ëµ¥ÊǼþ·Ç³£À§ÄѺÍÀË·Ñʱ¼äµÄÊÂ

In ASP.NET 2.0 the menu can be stored in a file to make it easier to maintain. This file is normally called web.sitemap, and is stored in the root directory of the web.
ÔÚASP.NET 2.0 ÖпÉÒÔ½«²Ëµ¥È«²¿±£´æÔÚÒ»¸öÎļþÖУ¬ÕâÑù»áÈÃά»¤¸ü¼ÓµÄÈÝÒס£Õâ¸öÎļþͨ³£±»½Ð×ö web.sitemap [ÍøÕ¾µØÍ¼] Õâ¸öÎļþͨ³£±£´æÔÚÍøÕ¾µÄ¸ùĿ¼Ï¡£

In addition, ASP.NET 2.0 has three new navigation controls:
ÁíÍ⣬ASP.NET 2.0 »¹ÓÐÈý¸öеĵ¼º½¿Ø¼þ£º

  • Dynamic menus
    ¶¯Ì¬²Ëµ¥
  • TreeViews
    Ê÷ÐÍ
  • Site Map Path
    ÍøÕ¾µØÍ¼Â·¾¶

The Sitemap File
ÍøÕ¾µØÍ¼Îļþ

The following sitemap file is used in this tutorial:
ÔÚÕâ¸ö½Ì³ÌÖн«Ê¹ÓÃÏÂÃæµÄÍøÕ¾µØÍ¼Îļþ£º

<?xml version="1.0" encoding="ISO-8859-1" ?>
<siteMap>
<siteMapNode title="Home" url="/aspnet/w3home.aspx">
<siteMapNode title="Services" url="/aspnet/w3services.aspx">
<siteMapNode title="Training" url="/aspnet/w3training.aspx"/>
<siteMapNode title="Support" url="/aspnet/w3support.aspx"/>
</siteMapNode>
</siteMapNode>
</siteMap>

Rules for creating a sitemap file:
½¨Á¢ÍøÕ¾µØÍ¼ÎļþÓÐÒÔÏÂÐèÒª×ñÑ­µÄ¹æÔò£º

  • The XML file must contain a <siteMap> tag surrounding the content
    ÆäΪXMLÎļþ£¬ÀïÃæ±ØÐ뽫ÄÚÈÝ·ÅÖÃÔÚ <siteMap> ±êÇ©ÄÚ
  • The <siteMap> tag can only have one <siteMapNode> child node (the "home" page)
    ÔÚ<siteMap> ±êÇ©ÖÐÖ»¿ÉÒÔÓÐÒ»¸ö <siteMapNode> ×ӽڵ㣨Ê×Ò³£©
  • Each <siteMapNode> can have several child nodes (web pages)
    ÿ¸ö<siteMapNode> ¿ÉÒÔÓжà¸ö×ӽڵ㠣¨Ò³Ã棩
  • Each <siteMapNode> has attributes defining page title and URL
    ÿ¸ö<siteMapNode> µÃÓж¨ÒåÒ³Ãæ±êÌâÒÔ¼°URLµÄÊôÐÔ

 Note: The sitemap file must be placed in the root directory of the web and the URL attributes must be relative to the root directory.
×¢Òâ£ºÍøÕ¾µØÍ¼Îļþ±ØÐë·ÅÔÚWEBµÄ¸ùĿ¼Ï£¬¶øÇÒURLÊôÐÔ±ØÐë¹ØÁªµ½¸ùĿ¼µØÖ·


Dynamic Menu
¶¯Ì¬²Ëµ¥

The <asp:Menu> control displays a standard site navigation menu.
<asp:Menu> ¿Ø¼ÓÏÔʾ±ê×¼µÄµ¼º½²Ëµ¥

Code Example:
´úÂë¾ÙÀý£º

<asp:SiteMapDataSource id="nav1" runat="server" />
<form runat="server">
<asp:Menu runat="server" DataSourceId="nav1" />
</form>

The <asp:Menu> control in the example above is a placeholder for a server created navigation menu.
ÔÚÉÏÃæ¾ÙÀýÖÐµÄ <asp:Menu> ¿Ø¼þΪ·þÎñÆ÷½¨Á¢µ¼º½²Ëµ¥Ëù±£ÁôµÄռλ·û¡£

The data source of the control is defined by the DataSourceId attribute. The id="nav1" connects it to the  <asp:SiteMapDataSource> control.
ͨ¹ýʹÓà DataSourceId ÊôÐÔÀ´Îª¿Ø¼þµÄÊý¾ÝÀ´Ô´×ö¶¨Òå¡£ id="nav1" ¾Í½«Ëü¸ú <asp:SiteMapDataSource> ¿Ø¼þÁ¬½ÓÆðÀ´ÁË¡£

The <asp:SiteMapDataSource> control automatically connects to the default sitemap file (web.sitemap).
<asp:SiteMapDataSource> ¿Ø¼þ»á×Ô¶¯µÄÓëĬÈϵÄÍøÕ¾µØÍ¼Îļþ£¨web.sitemap£©½øÐÐÁ¬½Ó

Click here to see a demo of Menu, TreeView, and SiteMapPath[²é¿´¸÷ÖÖµ¼º½²Ëµ¥Ð§¹û]


TreeView
Ê÷Ð½²Ëµ¥

The <asp:TreeView> control displays a multi level navigation menu.
<asp:TreeView> ¿Ø¼þ¿ÉÒÔÏÔʾ¶à¼¶µÄµ¼º½²Ëµ¥

The menu looks like a tree with branches that can be opened or closed with + or - symbol.
²Ëµ¥¿´ÉÏÈ¥ÊÇÊ÷×´µÄ£¬ÓпÉÒÔ¿ª¹ØµÄ·ÖÖ§£¬¿ª¹Ø²¿·ÖÓÉ + »ò - À´±ê¼Ç

Code Example:
´úÂë¾ÙÀý£º

<asp:SiteMapDataSource id="nav1" runat="server" />
<form runat="server">
<asp:TreeView runat="server" DataSourceId="nav1" />
</form>

The <asp:TreeView> control in the example above is a placeholder for a server created navigation menu.
<asp:TreeView> Õâ¸ö¿Ø¼þ¿ÉΪ·þÎñÆ÷½¨Á¢µ¼º½²Ëµ¥Ìṩռλ·û

The data source of the control is defined by the DataSourceId attribute. The id="nav1" connects it to the  <asp:SiteMapDataSource> control.

The <asp:SiteMapDataSource> control automatically connects to the default sitemap file (web.sitemap).

Click here to see a demo of Menu, TreeView, and SiteMapPath


SiteMapPath
ÍøÕ¾µØÍ¼Â·¾¶

The SiteMapPath control displays the trail (navigation path) to the current page. The path acts as clickable links to previous pages.
ÍøÕ¾µØÍ¼Â·¾¶¿Ø¼þ¿ÉÒÔÏÔʾÀ´µ½µ±Ç°Ò³ÃæµÄ×Ù¼££¨Â·¾¶µ¼º½£©¡£Â·¾¶µ£µ±¿ÉÒԻص½Ç°Ò³µÄ¿ÉµãÁ¬½Ó¡£

Unlike the TreeView and Menu control the SiteMapPath control does NOT use a SiteMapDataSource. The SiteMapPath control uses the web.sitemap file by default.
²»Í¬ÓÚÊ÷ÐͲ˵¥ºÍ²Ëµ¥¿Ø¼þ£¬ÍøÕ¾µØÍ¼¿Ø¼þ²»ÐèҪʹÓÃSiteMapDataSource[ÍøÕ¾µØÍ¼Êý¾ÝÔ´]¡£SiteMapPath ĬÈÏ»áʹÓà web.sitemapÎļþ

 Tips: If the SiteMapPath displays incorrectly, most likely there is an URL error (typo) in the web.sitemap file.
¼¼ÇÉ£ºÈç¹ûÍøÕ¾µØÍ¼Â·¾¶ÏÔʾ²»ÕýÈ·£¬´ó¶àÊýÇé¿öÊÇÒòΪÔÚweb.sitemapÎļþÖеÄURLÓдíÎ󣨴ò×ÖµÄʱºò£©

Code Example:
´úÂë¾ÙÀý

<form runat="server">
<asp:SiteMapPath runat="server" />
</form>

Click here to see a demo of Menu, TreeView, and SiteMapPath

ÆÀÂÛ (0) All