ASP. NET site navigation application details, asp.net site navigation

Source: Internet
Author: User

ASP. NET site navigation application details, asp.net site navigation

Navigation actually provides visitors with something similar to a map, allowing them to quickly find what they want.

An important concept introduced in 2.0 is an XML file such as siteMap, but its suffix is not XML.

To create a consistent and easy-to-manage navigation solution for a site, you can use asp.net site navigation. Asp.net provides the following functions:





After learning some basic knowledge, let's take a simple navigation example.

1. Create a master

Html code:

<% @ Master Language = "C #" AutoEventWireup = "true" CodeBehind = "mp. master. cs" Inherits = "Navigation. mp" %> <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN "" http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <Html xmlns =" http://www.w3.org/1999/xhtml "> <Head runat =" server "> <title> </title> <asp: ContentPlaceHolder ID =" head "runat =" server "> </asp: contentPlaceHolder> 

Display Effect:

2. Create the following navigation structure (apply the master above)

3. Add a site map (the name cannot be changed)

The code in Web. sitemap is as follows:

<? Xml version = "1.0" encoding = "UTF-8"?> <SiteMap xmlns =" http://schemas.microsoft.com/AspNet/SiteMap-File-1.0 "> <SiteMapNode url =". aspx "title =" Homepage "description =" 1 "> <siteMapNode url =" B. aspx "title =" root directory B page "description =" 2 "/> <siteMapNode url =" Default. aspx "title =" Default page "description =" 3 "/> <siteMapNode url =" "title =" one directory "description =" 4 "> <siteMapNode url = "~ /One/a. aspx "title =" a "description =" 5 "/> <siteMapNode url = "~ /One/B. aspx "title =" B "description =" 6 "/> <siteMapNode url =" "title =" once directory under one "description =" 7 "> <siteMapNode url = "~ /One/once/a. aspx "title =" a "description =" 8 "/> <siteMapNode url = "~ /One/once/B. aspx "title =" B "description =" 9 "/> </siteMapNode> <siteMapNode url =" "title =" two directory "description =" 10 "> <siteMapNode url = "~ /Two/a. aspx "title =" a "description =" 11 "/> <siteMapNode url = "~ /Two/B. aspx "title =" B "description =" 12 "/> </siteMapNode> <siteMapNode url =" sitemap. aspx "title =" Dynamic Modification in memory "description =" Dynamic Modification in Memory site map "/> </siteMapNode> </siteMap>

4. Put a treeView control in the root directory a. aspx. Create a map of the sites created by the data source.

After running, we can see the effect:

Next we will talk about dynamic modification of the site map in memory. What is dynamic modification of the site map in memory?

For example, there are usually many articles in the list, and we cannot put them all in the site map, what's more, the article ID, column number, product category, or product ID are not as expected in advance, so the site map does not exist and cannot be displayed outside. What should we do? This requires dynamic modification of the site map in the memory.
As mentioned above, we assume that there is an article that manages the system structure as follows:
Homepage-news-International News (list)
We can assume that the page at the International News level is list. aspx, and different columns have different IDs. The address structure is assumed as follows:
Xxx.com --~ /News/-- list. aspx? Id = 3 & page = 12
In this structure, we can write url = "~ /News/list. aspx ". So if the above structure is displayed when someone else browses, let's proceed.
Web. sitemap:

<? Xml version = "1.0" encoding = "UTF-8"?> <SiteMap xmlns =" http://schemas.microsoft.com/AspNet/SiteMap-File-1.0 "> <SiteMapNode url =". aspx "title =" Homepage "description =" 1 "> <siteMapNode url =" B. aspx "title =" root directory B page "description =" 2 "/> <siteMapNode url =" Default. aspx "title =" Default page "description =" 3 "/> <siteMapNode url =" "title =" one directory "description =" 4 "> <siteMapNode url = "~ /One/a. aspx "title =" a "description =" 5 "/> <siteMapNode url = "~ /One/B. aspx "title =" B "description =" 6 "/> <siteMapNode url =" "title =" once directory under one "description =" 7 "> <siteMapNode url = "~ /One/once/a. aspx "title =" a "description =" 8 "/> <siteMapNode url = "~ /One/once/B. aspx "title =" B "description =" 9 "/> </siteMapNode> <siteMapNode url =" "title =" two directory "description =" 10 "> <siteMapNode url = "~ /Two/a. aspx "title =" a "description =" 11 "/> <siteMapNode url = "~ /Two/B. aspx "title =" B "description =" 12 "/> </siteMapNode> <siteMapNode url =" sitemap. aspx "title =" Dynamic Modification in memory "description =" Dynamic Modification in Memory site map "/> </siteMapNode> </siteMap>

This statement uses the title to dynamically modify the memory.
Then, place a SitemapPath control in sitemap. aspx on the demo page. set its renderCurrentNodeAsLink to true (indicating that the current node is a link)
Background code of the page:

Using System; using System. collections. generic; using System. linq; using System. web; using System. web. UI; using System. web. UI. webControls; namespace Navigation {public partial class sitemap: System. web. UI. page {protected void Page_Load (object sender, EventArgs e) {// first, register an event here, that is, trigger this event SiteMap when accessing the CurrentNode attribute. siteMapResolve + = new SiteMapResolveEventHandler (SiteMap_SiteMapResolve);} Site MapNode SiteMap_SiteMapResolve (object sender, SiteMapResolveEventArgs e) {// obtain and create a copy of the current node in our custom processing method, and copy its parent node SiteMapNode CurrentNode = SiteMap. currentNode. clone (true); SiteMapNode TempNode = CurrentNode; int id = nid (); int page = npage (); // obtain the variable if (id> 0) from the method below) {TempNode. url = TempNode. url + "? Id = "+ id. toString () ;}if (id> 0 & page> = 0) {TempNode. url = TempNode. url + "& page =" + page. toString ();} else if (page> 0) {TempNode. url = TempNode. url + "? Page = "+ page. ToString ();} return TempNode;} // set a random number. Private int nid () {return 3 ;}private int npage () {return 12 ;}}}

Explanation: The above nid () and npage () are two methods used to obtain parameters, which are determined based on the actual situation.
Run:

At this point, we have implemented the dynamic modification of the site map in the memory!
Site navigation is very commonly used in BS development, so we still need to have a deep understanding of it. Next we will send related articles to discuss and learn with you!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.