Use the sitemappath Control for Forum navigation (also suitable for other systems)

Source: Internet
Author: User

First, the Forum is very simple. It is made of three webpages, all of which are made using one template.

The first webpage (default. aspx) is used to display the category of the Forum. Click the category to upload the categoryid to the next page.

The second webpage (list. aspx) is used to display the list of articles under a certain category. Use the categoryid parameter to differentiate. Such as list. aspx? Categoryid = 1 indicates the first class, list. aspx? Categoryid = 2 indicates the second largest class. Click the corresponding article and upload the article number (TID) to the next webpage.

Third web page (content. aspx): used to display the content of the article and the reply content. Varies according to the TID parameter. Such as content. aspx? Tid = 1

Parameters are used in the second and third pages. Therefore, it is difficult to use the sitemappath Control for navigation, but we can dynamically modify the URL of the current node and the parent node.

Step 1: Compile web. sitemap

<? XML version = "1.0" encoding = "UTF-8"?>
<Sitemap xmlns = "http://schemas.microsoft.com/AspNet/SiteMap-File-1.0">
<Sitemapnode url = "default. aspx" Title = "Homepage" Description = "Homepage">
<Sitemapnode url = "list. aspx? Categoryid = 1 "Title =" Asp.net "Description =" website development ">
<Sitemapnode url = "content. aspx" Title = "Article body"/>
</Sitemapnode>
<Sitemapnode url = "list. aspx? Categoryid = 2 "Title =" PowerBuilder "Description =" Information System Development ">
<Sitemapnode url = "" Title = "Article body"/>
</Sitemapnode>
<Sitemapnode url = "list. aspx? Categoryid = 3 "Title =" English "Description =" English learning ">
<Sitemapnode url = "" Title = "Article body"/>
</Sitemapnode>
</Sitemapnode>
</Sitemap>

Here, there are only three categories in my Forum: Asp.net, PowerBuilder, and English. The second-level navigation can be written here because there are few categories. However, for Level 3 navigation, there are thousands of articles and it is impossible to write them all. Therefore, I only need to write the url = "content. aspx" once. The rest is empty, so that the third-level navigation is displayed on the webpage.

Step 2: Modify the template, insert the sitemappath control, and modify its rendercurrentnodeaslink = "true". Note that this is very important.

Step 3: modify the content. ASPX page. The sitemappath control of the other two webpages can be normally displayed, so there is no need to modify it.

First, register the event in page_load.

Protected void page_load (Object sender, eventargs E)
{
If (! Ispostback)
BIND ();
// Register the sitemap event
Sitemap. sitemapresolve + = new sitemapresolveeventhandler (sitemap_sitemapresolve );
}

Next, write the Event code:

Private sitemapnode sitemap_sitemapresolve (Object sender, sitemapresolveeventargs E)
{
// Clone the current node and its parent node
Sitemapnode currentnode = sitemap. currentnode. Clone (true );
// Obtain and compile the article
Int tid = convert. toint32 (request. querystring ["TID"]);
If (0! = Tid)
{
// Modify the URL of the current node
Currentnode. url = currentnode. url + "? Tid = "+ tid. tostring ();
// Getcid (TID) is a self-written method that returns the category number through the document number
Int categoryid = GETID (TID );
// Modify the URL of the parent node
Currentnode. parentnode. url = "list. aspx? Categoryid = "+ categoryid. tostring ();
// Modify the title of the parent node. getname (categoryid) is also a self-compiled method. You can obtain the category name by using the category number.
Currentnode. parentnode. Title = getname (categoryid );
}
Return currentnode;
}

Note: You must modify the URL and title of the parent node. Otherwise, the display of your sitemappath control will be incorrect.

Finally, in order not to affect other web pages, log off the event in page_unload:

 protected void Page_Unload(object sender, EventArgs e)
{
SiteMap.SiteMapResolve -= new SiteMapResolveEventHandler(SiteMap_SiteMapResolve);
}

Now, the navigation is complete. No matter how the URL changes, we can modify it dynamically to serve us.

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.