ASP.NET Web Forms - Navigation
ASP.NET comes with built-in navigation controls.
Website Navigation
Maintaining the menu of a large website is difficult and time-consuming.
In ASP.NET, menus can be stored in a file, making them easy to maintain. The file is usually named web.sitemapand is stored in the root directory of the website.
In addition, ASP.NET has three new navigation controls:
- Dynamic menus
- TreeViews
- Site Map Path
Sitemap File
In this tutorial, the following sitemap file is used:
Rules for creating a sitemap file:
- The XML file must contain the surrounding the content
tag -
The tag can have only one child node (the "home" page) - Each
can have multiple child nodes (web pages) - Each
with attributes that define the page title and URL
**Note:** The sitemap file must be located in the site root directory, and the URL attribute must be relative to that root directory.
Dynamic Menu
asp:Menu control can display a standard site navigation menu.
Code example:
In the example above, the asp:Menu control is a placeholder for the server to create a navigation menu.
The data source of the control is defined by the DataSourceId attribute. id="nav1" Connect the data source to the asp:SiteMapDataSource control.
asp:SiteMapDataSource The control automatically connects to the default sitemap file (web.sitemap)。
TreeView
asp:TreeView control can display a multi-level navigation menu.
This kind of menu looks like a tree with branches and leaves, which can be opened or closed using the + or - symbols.
Code example:
In the example above, the asp:TreeView control is a placeholder for the server to create a navigation menu.
The data source of the control is defined by the DataSourceId attribute. id="nav1" Connect the data source to the asp:SiteMapDataSource control.
asp:SiteMapDataSource The control automatically connects to the default sitemap file (web.sitemap)。
SiteMapPath
The SiteMapPath control can display a pointer to the current page (navigation path). The path is displayed as clickable links pointing to the parent pages.
Unlike the TreeView and Menu controls, the SiteMapPath controldoes not use SiteMapDataSource. The SiteMapPath control uses the web.sitemap file by default.
Tip: If SiteMapPath does not display correctly, it is most likely due to a URL error (typo) in the web.sitemap file.
Code example:
In the example above, the asp:SiteMapPath control is a placeholder for the server to create a navigation menu.