w3pop.com :: ÍøÂçѧԺ :: ASP.NET :: ASP.NET 2.0 - Navigation µ¼º½
ASP.NET 2.0 has built-in navigation controls
ÔÚASP.NET2.0ÖÐÄÚÖÃÁ˵¼º½¿Ø¼þ
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 »¹ÓÐÈý¸öеĵ¼º½¿Ø¼þ£º
The following sitemap file is used in this tutorial:
ÔÚÕâ¸ö½Ì³ÌÖн«Ê¹ÓÃÏÂÃæµÄÍøÕ¾µØÍ¼Îļþ£º
<?xml version="1.0" encoding="ISO-8859-1" ?> |
Rules for creating a sitemap file:
½¨Á¢ÍøÕ¾µØÍ¼ÎļþÓÐÒÔÏÂÐèÒª×ñѵĹæÔò£º
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ÊôÐÔ±ØÐë¹ØÁªµ½¸ùĿ¼µØÖ·
The <asp:Menu> control displays a standard site navigation menu.
<asp:Menu> ¿Ø¼ÓÏÔʾ±ê×¼µÄµ¼º½²Ëµ¥
Code Example:
´úÂë¾ÙÀý£º
<asp:SiteMapDataSource id="nav1" runat="server" /> <form runat="server"> |
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[²é¿´¸÷ÖÖµ¼º½²Ëµ¥Ð§¹û]
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"> |
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
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