ASP.NET Web Forms - Navigation
ASP.NET comes with built-in navigation controls.
Website Navigation
Maintaining menus for large websites can be difficult and time-consuming.
In ASP.NET, menus can be stored in a file, making them easy to maintain. The file is typically named web.sitemap and is stored in the root directory of the website.
Additionally, ASP.NET has three new navigation controls:
- Dynamic menus
- TreeViews
- Site Map Path
Sitemap File
In this tutorial, we use the following sitemap file:
Rules for creating a sitemap file:
- The XML file must contain a <siteMap> tag surrounding the content.
- The <siteMap> tag can only have one <siteMapNode> child node (the "home" page).
- Each <siteMapNode> can have multiple child nodes (web pages).
- Each <siteMapNode> has attributes defining the page title and URL.
Note: The sitemap file must be located in the site's root directory, and the URL attributes must be relative to this root directory.
Dynamic Menu
The <asp:Menu> control can display a standard site navigation menu.
Code Example:
The <asp:Menu> control in the example above is a placeholder for the server to create a navigation menu.
The data source for the control is defined by the DataSourceId property. id="nav1" connects the data source to the <asp:SiteMapDataSource> control.
The <asp:SiteMapDataSource> control automatically connects to the default sitemap file (web.sitemap).
TreeView
The <asp:TreeView> control can display a multi-level navigation menu.
This menu looks like a tree with branches and leaves, and can be opened or closed with + or - symbols.
Code Example:
The <asp:TreeView> control in the example above is a placeholder for the server to create a navigation menu.
The data source for the control is defined by the DataSourceId property. id="nav1" connects the data source to the <asp:SiteMapDataSource> control.
The <asp:SiteMapDataSource> control automatically connects to the default sitemap file (web.sitemap).
SiteMapPath
The SiteMapPath control displays a pointer to the current page (navigation path). This path is shown as clickable links pointing to the parent pages.
Unlike the TreeView and Menu controls, the SiteMapPath control does not use SiteMapDataSource. The SiteMapPath control defaults to using the web.sitemap file.
Code Example:
The <asp:SiteMapPath> control in the example above is a placeholder for the server to create a navigation menu.