In the past few days, I found my predecessors in the garden about ASP. NET site navigation. I found it for a long time, but it was quite rewarding! However, I want to extract my own knowledge about ASP. NET site navigation controls for later use:
Sitemappath: displays a set of text or image hyperlinks, allowing you to locate a website with minimal page space.
SitemappathA widget is a site navigation control that reflects the data provided by the sitemap object. It provides a space-saving method for easily locating the site and serves as a reference point for the location of the current display page in the site. This type of control is usually called bread or eyebrow because it displays the hierarchical path of the hyperlink Page name, thus providing a jump from the current position along the page hierarchy. Sitemapdatasource.SitemappathIt is useful for sites with deep hierarchical page structures. In such sites, Treeview or menu may require more page space.
SitemappathThe widget directly uses the site map data of the website. If it is used on a page that is not displayed in the site map, it is not displayed. For more information about site maps, see .
SitemappathIt is composed of nodes. Each element in the path is called a node and is represented by a sitemapnodeitem object. The root node of the layered tree is called the root node. Indicates that the node on the current page is called the current node. Any other node between the current node and the root node is the parent node. The following table describes three different node types.
|
Node Type |
Description |
|
Root Node |
Anchor specifies a node in a node hierarchy. |
|
Parent node |
There are one or more subnodes but not the current node. |
|
Current Node |
The node of the current display page. |
SitemappathEach node displayed is a hyperlink or literal control. You can apply a template or style to these two controls. The node Application Template and style must follow two priority rules:
-
If a template is defined for a node, It is rewritten to the style defined by the node.
-
The template and style specific to the node type will be rewritten to the general template and style defined for all nodes.
The nodestyle and nodetemplate attributes apply to all nodes, regardless of the node type. If both attributes are defined, nodetemplate is used first.
The currentnodetemplate and currentnodestyle attributes apply to nodes that represent the current display page. If nodetemplate is defined in addition to currentnodetemplate, it is ignored. If nodestyle is defined in addition to currentnodestyle, It is merged with currentnodestyle to create the merged style. The merged style uses all elements of currentnodestyle and any additional elements in nodestyle that do not conflict with currentnodestyle.
The rootnodetemplate and rootnodestyle attributes apply to nodes that represent the root of the site navigation hierarchy. If you define nodetemplate in addition to rootnodetemplate, it is ignored. If nodestyle is defined in addition to rootnodestyle, It is merged with rootnodestyle to create the merged style. This merging style uses all the elements of rootnodestyle and any additional elements in nodestyle that do not conflict with currentnodestyle. Finally, if the current display page is the root page of the site, rootnodetemplate and rootnodestyle are used instead of currentnodetemplate or currentnodestyle.
SitemappathThe control will be provided by the site map marked by the sitemapprovider PropertyProgramThe data source used as the site navigation information. If no provider is specified, it uses the default provider of the site, which is provided by sitemap..::.Provider attribute identifier. Generally, this is an instance of the default site map provider (xmlsitemapprovider) in ASP. NET. If you useSitemappathControl, but the site map provider is not configured, this control will cause an httpexception.
SitemappathControls also provide multiple events that you can program. This allows you to run a custom routine every time an event occurs. The following table listsSitemappathEvents supported by the control.
|
Event |
Description |
|
Itemcreated |
SitemappathThe control first creates a sitemapnodeitem and then associates it with sitemapnode. |
|
Itemdatabound |
It occurs when sitemapnodeitem is bound to the site map data contained in sitemapnode. |
Derived fromSitemappathClass will override the initializeitem method to customize the sitemapnodeitem control contained in the navigation control. To fully control the creation method of the sitemapnodeitem object and add itSitemappathThe derived class will override the createcontrolhierarchy method.
The followingCodeThe example shows how to extendSitemappathControl and add new features to it.DropdownsitemappathAdd a dropdownlist after the current node to make it easy to locate the subnode page on the current page. This example shows how to use the sitemapnodeitem object after creating an item, including checking their sitemapnodeitemtype and calling the onitemcreated method.
Code
Using System;
Using System. collections;
Using System. componentmodel;
Using System. Security. permissions;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. webcontrols;
// The dropdownnavigationpath is a class that extends the sitemappath
// Control and renders a dropdownlist after the currentnode.
// Dropdownlist displays a list of pages found further down the site map
// Hierarchy from the current one. Selecting an item in the dropdownlist
// Redirects to that page.
//
// For simplicity, the dropdownnavigationpath assumes
// Roottocurrent pathdirection, and does not apply styles
// Or templates the current node.
//
[Aspnethostingpermission (securityaction. Demand, Level = Aspnethostingpermissionlevel. Minimal)]
Public Class Dropdownnavigationpath: sitemappath {
// Override the initializeitem method to add a pathseparator
// And dropdownlist to the current node.
Protected Override Void Initializeitem (sitemapnodeitem item ){
//The only node that must be handled is the currentnode.
If(Item. itemtype=Sitemapnodeitemtype. Current)
{
Hyperlink HLINK= NewHyperlink ();
// No theming for the hyperlink.
Hlink. enabletheming = False ;
// Enable the link of the sitemappath is enabled.
Hlink. Enabled = This . Enabled;
// Set the properties of the hyperlink
// Match those of the corresponding sitemapnode.
Hlink. navigateurl = Item. sitemapnode. url;
Hlink. Text = Item. sitemapnode. title;
If (Showtooltips ){
Hlink. tooltip = Item. sitemapnode. description;
}
//Apply styles or templates to the hyperlink here.
//
//
// Add the item to the controls collection.
item. controls. add (hlink);
adddropdownlistaftercurrentnode (item);
}< br> else {< br> base . initializeitem (item);
}< BR >}< br> private void adddropdownlistaftercurrentnode (sitemapnodeitem item) {
Sitemapnodecollection childnodes=Item. sitemapnode. childnodes;
//Only do this work if there are child nodes.
If(Childnodes! = Null){
// Add another pathseparator after the currentnode.
sitemapnodeitem finalseparator =
New sitemapnodeitem (item. itemindex,
sitemapnodeitemtype. pathseparator);
sitemapnodeitemeventargs eventargs =
New sitemapnodeitemeventargs (finalseparator);
initializeitem (finalseparator);
// call onitemcreated every time a sitemapnodeitem is
// created and initialized.
onitemcreated (eventargs );
//The pathseparator does not bind to any sitemapnode, so
//Do not call databind on the sitemapnodeitem.
Item. Controls. Add (finalseparator );
// Create a dropdownlist and populate it with the children of
// Currentnode. There are no styles or templates that are applied
// To the dropdownlist control. If onselectedindexchanged is raised,
// The event handler redirects to the page selected.
// The currentnode has child nodes.
Dropdownlist ddlist = New Dropdownlist ();
Ddlist. autopostback = True ;
Ddlist. selectedindexchanged+ = NewEventhandler (This. Dropdownnavpatheventhandler );
// Add a listitem to the dropdownlist for every node in
// Sitemapnodes collection.
Foreach (Sitemapnode Node In Childnodes ){
Ddlist. Items. Add ( New Listitem (node. Title, node. url ));
}
item. Controls. Add (ddlist);
}< BR >}
// the sender is the dropdownlist.
private void dropdownnavpatheventhandler ( Object sender, eventargs e) {
dropdownlist DDL = sender as dropdownlist;
//Redirect to the page the user chose.
If(Context! = Null)
Context. response. Redirect (DDL. selectedvalue );
}
}
Role nodes in Web. sitemap and web. config:
1. Only users with a role of 2 can see this node.
<Sitemapnode Title = "parent=child1" url = "~ /Parent1/parent1_child1.aspx "roles =" 2 "/>
2,Only users with roles 1 or 2 can operate files in this folder.
<Location Path = "parent2">
<System. Web>
<Authorization>
<Allow roles = "1, 2"/>
<Deny users = "*"/>
</Authorization>
</System. Web>
</Location>
For example, when a member logs in, his/her personal interface has a menu with all the functions that can be seen in the menu. However, some functions can only be used after payment is made, you must set the roles attribute.