ASP.NET Web Forms - Navigation
This section mainly introduces ASP.NET website navigation and how to use navigation controls.ASP.NET comes with built-in navigation controls.
Site navigation
Maintaining menus for large websites is difficult and time-consuming.
In ASP.NET, menus can be stored in files, making them easier to maintain. The file is usually named web.sitemap and is stored in the root directory of the website.
Additionally, ASP.NET has three central navigation controls:
Dynamic menus
TreeViews
Site Map Path
Sitemap file
In this tutorial, the following sitemap file is used:
<?xml version="1.0" encoding="ISO-8859-1" ?><siteMap><siteMapNode url="/aspnet/w3home.aspx"><siteMapNode url="/aspnet/w3services.aspx"><siteMapNode url="/aspnet/w3training.aspx"/><siteMapNode url="/aspnet/w3support.aspx"/></siteMapNode></siteMapNode></siteMap>Rules for creating sitemap files:
The XML file must contain <siteMap> tags surrounding the content
The <siteMap> tag can only have one <siteMapNode> child node ("home" page)
Each <siteMapNode> can have multiple child nodes (web pages)
Each <siteMapNode> has attributes that define the page title and URL
Note: The sitemap file must be located in the site root directory, and the URL properties must be relative to that root directory.
dynamic menu
The <asp:Menu> control displays a standard site navigation 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 above example is a placeholder for the server to create a navigation menu.
The control's data source 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 displays a multi-level navigation menu.
This menu looks like a tree with branches and can be opened or closed using the + or - symbols.
Code example:
<asp:SiteMapDataSource id="nav1" runat="server" /><form runat="server"><asp:TreeView runat="server" DataSourceId="nav1" /></form>The <asp:TreeView> control in the above example is a placeholder for the server to create a navigation menu.
The control's data source 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 (navigation path) to the current page. The path appears as a clickable link to the parent page.
Unlike the TreeView and Menu controls, the SiteMapPath control does not use a SiteMapDataSource. The SiteMapPath control uses the web.sitemap file by default.
Tip: If the SiteMapPath does not display correctly, it is most likely due to a URL error (printing error) in the web.sitemap file.
Code example:
<form runat="server"><asp:SiteMapPath runat="server" /></form>The <asp:SiteMapPath> control in the above example is a placeholder for the server to create a navigation menu.
The above is about the use of ASP.NET navigation. Through website navigation, you can quickly access a page.