Establish site navigation hierarchy in asp.net 2.0 (2)

Source: Internet
Author: User
Tags end net reference return access

In the following example, the TreeView and menu controls you see Configure some style properties for different areas and operations, such as NodeStyle and HoverNodeStyle. The TreeView and Menu controls in the example are connected to the same SiteMapDataSource control on the page. The SiteMapPath control accesses the same data through the default SiteMapProvider set in the Web.config file.

The following is a reference fragment:
<asp:treeview id= "TreeView1" forecolor= "white" datasourceid= "SiteMapDataSource1" nodeindent= "0" Nodestyle-childnodespadding= "Ten" runat= "Server" >
<LevelStyles>
<asp:treenodestyle font-bold= "true"/>
<asp:treenodestyle/>
<asp:treenodestyle font-size= "X-small"/>
</LevelStyles>
<nodestyle forecolor= "White" horizontalpadding= "5"
<selectednodestyle backcolor= "LightBlue" forecolor= "Blue"/>
</asp:treeview>
<asp:sitemappath id= "SiteMapPath1" runat= "Server"/>
<asp:menu id= "Menu1" datasourceid= "SiteMapDataSource1" runat= "Server"
<dynamicselectedstyle backcolor= "LightBlue" forecolor= "Blue"/>
<dynamichoverstyle font-underline= "true"/>
<statichoverstyle font-underline= "true"/>
</asp:Menu>
<asp:sitemapdatasource id= "SiteMapDataSource1" runat= "Server"/>

Using the Site Navigation API

The site Navigation API is a programmatic abstraction that uses a configurable provider to access navigation data. The site navigation provider isolates the storage details of navigation data from other parts of the API. The Site navigation API exposes navigation data through Sitemap and SiteMapNode classes. The Sitemap class returns the SiteMapNode instance corresponding to the current page. It also has access to the providers that are configured for site navigation features. SiteMapProvider provides a rich API for performing some of the following transactions:

· Retrieves the SiteMapNode instance based on the current HttpContext or any URL.

· Retrieves the parent or child node of the SiteMapNode.

· Accesses the SiteMapNode of the current page and the root SiteMapNode in the entire navigation hierarchy.

· Execute authorization rules, which ensures that the provider only returns nodes that the user can see.

The basic navigational information and features exposed by the SiteMapNode instance include:

· URLs, title and description properties, and custom attributes that developers add to SiteMapNode.

· Gets the parent and child nodes of a node.

· Navigate between the front and back nodes of a node.

· Gets a pointer to the SiteMapProvider instance, which returns a node.

Asp. NET is published with the XmlSiteMapProvider provider. The provider uses the data in the XML file (Web.sitemap) and returns SiteMapNode instances based on the data. XmlSiteMapProvider has some of the following features:

· Multiple site Map (sitemap) files can be linked together to build a "virtual" navigation data collection.

· Multiple XmlSiteMapProvider instances can be linked together to build a "virtual" navigation data collection.

· The provider can return the overly-worrying node based on the current file authorization and URL authorization rules for the site.

With the SiteMapProvider pointer, you can retrieve the site navigation data for a specific node based on the URL. It will let you get the SiteMapNode instance pointer in the site navigation data. The ability to retrieve arbitrary SiteMapNode instances, combined with the ability to start site navigation from any SiteMapNode, allows you to easily traverse the navigation data of the site.

As a developer, you can also store navigation data in other formats (for example, as relational data is stored in a database). Then you should build a custom provider that derives from SiteMapProvider.

Programming with site Navigation classes

You can programmatically obtain navigation data in your code. The starting point for programmatic access to site navigation data is the Sitemap class. There are a number of static methods in this class, the most important of which is the CurrentNode attribute. On any page of the site, you can invoke Sitemap.currentnode to refer to a piece of navigation data that matches the current execution page. The navigation data is returned in the form of a SiteMapNode instance-when you call Sitemap.currentnode, the property returns the SiteMapNode instance corresponding to the current page. The site navigation feature returns the correct node based on the navigation data stored in the XML file.

The following example shows a user control with a simple paging feature. In the displayed page, the user control is located in the middle of the bottom of the page. Initially, the content of the link is "next topic." When you click on this link, the user control invokes the SiteMapNode object to detect if there is a page near the current page. The code detects the Sitemap.currentnode property to see if it has a page (SiteMap.CurrentNode.PreviousSibling) before it, and whether it has a page behind it ( SiteMap.CurrentNode.NextSibling). If a page exists, the user control displays a hyperlink and sets the NavigateUrl property of the hyperlink to the URL property of a nearby node.

If you click on the TreeView link on the left side of the page, you can see how the user control automatically displays the appropriate "previous topic" and "later theme" links. The user control also displays a different hyperlink, which you can click to return to the home page. If you look at how this hyperlink works, you'll find that the control leverages the custom attribute "CustomAttribute" in the home page element. The control demonstrates how to use the SiteMapNode default indexer to retrieve the value of a custom property.

The following is a reference fragment:

If (not SiteMap.CurrentNode.PreviousSibling are nothing)
Prevtopic.navigateurl = SiteMap.CurrentNode.PreviousSibling.Url
Else
Prevtopic.visible = False
Separator.visible = False
End If

' Use Findsitemapnode to find the nodes in the URL and extract some information
Dim RootNode as SiteMapNode = SiteMap.Provider.FindSiteMapNode ("~/home.aspx")
Gohome.navigateurl = Rootnode.url
Gohome.tooltip = Rootnode.description
Gohome.text = RootNode ("CustomAttribute")
End Sub



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.