Home>

Navigation actually gives visitors a map-like thing,Let visitors find what they want faster.

An important concept was introduced in 2.0,That is an xml file such as sitemap, but its suffix is ​​not xml.

To create a consistent, easy-to-manage navigation solution for your site,You can use asp.net site navigation.asp.net site navigation provides the following features:

After learning some basics,Let's make a simple navigation example.

1.First 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 runat="server">
 </asp:contentplaceholder>
</head>
<body>
 <form runat="server">
 <div>
  <table>
   <tr>
    <td>
      Your current position is:
     <asp:sitemappath runat="server" font-names="verdana"
      font-size="0.8em" pathseparator=":">
      <currentnodestyle forecolor="#333333" />
      <nodestyle font-bold="true" forecolor="#990000" />
      <pathseparatorstyle font-bold="true" forecolor="#990000" />
      <rootnodestyle font-bold="true" forecolor="#ff8000" />
     </asp:sitemappath>
    </td>
   </tr>
   <tr>
    <td>
     <asp:contentplaceholder runat="server">
     </asp:contentplaceholder>
    </td>
   </tr>
   <tr>
    <td>
     all rights reserved . . . . . . .</td>
   </tr>
  </table>
 </div>
 </form>
</body>
</html>

Displayed effect:

2. Establish the following navigation structure (the master on the unified application)

3. Add sitemap (name cannot be changed)

The code under 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="a.aspx" description="1">
  <sitemapnode url="b.aspx" description="2" />
  <sitemapnode url="default.aspx" description="3" />
  <sitemapnode url="" description="4">
  <sitemapnode url="~/one/a.aspx" description="5" />
  <sitemapnode url="~/one/b.aspx" description="6" />
  <sitemapnode url="" description="7">
   <sitemapnode url="~/one/once/a.aspx" description="8" />
   <sitemapnode url="~/one/once/b.aspx" description="9" />
  </sitemapnode>
  </sitemapnode>
  <sitemapnode url="" description="10">
  <sitemapnode url="~/two/a.aspx" description="11" />
  <sitemapnode url="~/two/b.aspx" description="12" />
  </sitemapnode>
  <sitemapnode url="sitemap.aspx" description="Dynamic modification of the sitemap in memory" />
 </sitemapnode>
</sitemap>

4, in the root directory a.aspx, put a treeview control. The new data source is the site map we established.

After running, we can see the effect as shown:

Next I will talk about dynamically modifying the site map in memory.What is the dynamic modification of the site map in memory,What exactly does it mean?

For example, there are usually multiple articles in the article list.We ca n’t put all this in the site map,Moreover, the article id, column number, product category or product id are not what we can anticipate in advance,So it ’s not in the site map,Cannot be displayed outside,what can we do about it?This requires dynamic modification of the site map in memory.

As mentioned above,We assume that an article management system is structured as follows:

Home-News-International News (list)

Then it can be assumed that the page of the level of international news is list.aspx, and different columns have different ids, then the address structure is assumed as follows:

xxx.com-- ~/news /-list.aspx?id=3&page=12

Such a structure,Our processing of the news list page can only statically write url="~/news/list.aspx" in the site map. So if the above structure appears when others browse,We deal with it next.

web.sitemap:

<?xml version="1.0" encoding="utf-8"?>
<sitemap xmlns="http://schemas.microsoft.com/aspnet/sitemap-file-1.0">
 <sitemapnode url="a.aspx" description="1">
  <sitemapnode url="b.aspx" description="2" />
  <sitemapnode url="default.aspx" description="3" />
  <sitemapnode url="" description="4">
  <sitemapnode url="~/one/a.aspx" description="5" />
  <sitemapnode url="~/one/b.aspx" description="6" />
  <sitemapnode url="" description="7">
   <sitemapnode url="~/one/once/a.aspx" description="8" />
   <sitemapnode url="~/one/once/b.aspx" description="9" />
  </sitemapnode>
  </sitemapnode>
  <sitemapnode url="" description="10">
  <sitemapnode url="~/two/a.aspx" description="11" />
  <sitemapnode url="~/two/b.aspx" description="12" />
  </sitemapnode>
  <sitemapnode url="sitemap.aspx" description="Dynamic modification of the sitemap in memory" />
 </sitemapnode>
</sitemap>

The main use of this is to dynamically modify the memory in the title to demonstrate.

Then put a sitemappath control in the sitemap.aspx demo page, pay attention to set its rendercurrentnodeaslink to true (indicate that the current node is a link)

Page background code:

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 we register an event here,In other words, this event is triggered when the currentnode property is accessed.
   sitemap.sitemapresolve +=new sitemapresolveeventhandler (sitemap_sitemapresolve);
  }
  sitemapnode sitemap_sitemapresolve (object sender, sitemapresolveeventargs e)
  {
   //Our custom processing method gets and creates a copy of the current node,And at the same time copy his parent node
   sitemapnode currentnode=sitemap.currentnode.clone (true);
   sitemapnode tempnode=currentnode;
   int id=nid ();
   int page=npage ();
   //Get variables from
   if (id>0)
   {
    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 number at will.
  private int nid ()
  {
   return 3;
  }
  private int npage ()
  {
   return 12;
  }
 }
}

Explanation:The above nid () and npage () are the two methods used to get parameters,It depends on the actual situation in practice.

Running, we look at the properties of this link:

At this point, we have simply implemented the dynamic modification of the site map in memory!

  • Previous The basic naming methods and values ​​of variables and their references
  • Next Mysql/MariaDB is in a progress bar state during startup and causes the failure and the solution