ASP.NET站点导航(二)

这几天闲来无事,就在园子里找前辈们关于ASP.NET站点导航的东西,找了好半天,不过颇有收获,呵呵!但是我想先把我自己看到的关于ASP.NET站点导航控件的知识摘抄下来,以备后用:

SiteMapPath:显示一组文本或图像超链接,使您可以在使用最少页面空间的同时更轻松地定位网站。

SiteMapPath 控件是一种站点导航控件,反映 SiteMap 对象提供的数据。它提供了一种用于轻松定位站点的节省空间方式,用作当前显示页在站点中位置的引用点。此种类型的控件通常称为面包屑或眉毛,因为它显示了超链接页名称的分层路径,从而提供了从当前位置沿页层次结构向上的跳转。SiteMapDataSource。SiteMapPath 对于分层页结构较深的站点很有用,在此类站点中 TreeView 或 Menu 可能需要较多的页空间。

SiteMapPath 控件直接使用网站的站点地图数据。如果将其用在未在站点地图中表示的页面上,则其不会显示。有关站点地图的更多信息,请参见 。

SiteMapPath 由节点组成。路径中的每个元素均称为节点,用 SiteMapNodeItem 对象表示。锚定路径并表示分层树的根的节点称为根节点。表示当前显示页的节点称为当前节点。当前节点与根节点之间的任何其他节点都为父节点。下表描述了三种不同的节点类型。

节点类型

说明

根节点

锚定节点分层组的节点。

父节点

有一个或多个子节点但不是当前节点的节点。

当前节点

表示当前显示页的节点。

SiteMapPath 显示的每个节点都是 HyperLink 或 Literal 控件,您可以将模板或样式应用到这两种控件。对节点应用模板和样式需遵循两个优先级规则:

  • 如果为节点定义了模板,它会重写为节点定义的样式。

  • 特定于节点类型的模板和样式会重写为所有节点定义的常规模板和样式。

NodeStyle 和 NodeTemplate 属性适用于所有节点,而不考虑节点类型。如果同时定义了这两个属性,将优先使用 NodeTemplate。

CurrentNodeTemplate 和 CurrentNodeStyle 属性适用于表示当前显示页的节点。如果除了 CurrentNodeTemplate 外,还定义了 NodeTemplate,则将忽略它。如果除了 CurrentNodeStyle 外,还定义了 NodeStyle,则它将与 CurrentNodeStyle 合并,从而创建合并样式。此合并样式使用 CurrentNodeStyle 的所有元素,以及 NodeStyle 中不与 CurrentNodeStyle 冲突的任何附加元素。

RootNodeTemplate 和 RootNodeStyle 属性适用于表示站点导航层次结构根的节点。如果除了 RootNodeTemplate 外,还定义了 NodeTemplate,则将忽略它。如果除了 RootNodeStyle 外,还定义了 NodeStyle,则它将与 RootNodeStyle 合并,从而创建合并样式。此合并样式使用 RootNodeStyle 的所有元素,以及 NodeStyle 中不与 CurrentNodeStyle 冲突的任何附加元素。最后,如果当前显示页是该站点的根页,将使用 RootNodeTemplate 和 RootNodeStyle,而不是 CurrentNodeTemplate 或 CurrentNodeStyle。

SiteMapPath 控件将由 SiteMapProvider 属性标识的站点地图提供程序用作站点导航信息的数据源。如果未指定提供程序,它将使用站点的默认提供程序,此提供程序由 SiteMap..::.Provider 属性标识。通常,这是 ASP.NET 默认站点地图提供程序(即 XmlSiteMapProvider)的一个实例。如果在站点内使用了 SiteMapPath 控件,但未配置站点地图提供程序,该控件将引发 HttpException 异常。

SiteMapPath 控件还提供多个您可以对其进行编程的事件。这使您可以在每次发生事件时都运行一个自定义例程。下表列出了 SiteMapPath 控件支持的事件。

事件

说明

ItemCreated

SiteMapPath 控件先创建一个 SiteMapNodeItem,然后将其与 SiteMapNode 关联时发生。

ItemDataBound

将 SiteMapNodeItem 绑定到 SiteMapNode 包含的站点地图数据时发生。

派生自 SiteMapPath 的类会重写 InitializeItem 方法,以自定义导航控件包含的 SiteMapNodeItem 控件。为了完全控制 SiteMapNodeItem 对象的创建方式以及将其添加到 SiteMapPath 的方式,派生类会重写 CreateControlHierarchy 方法。

下面的代码示例演示如何通过重写 InitializeItem 方法,扩展 SiteMapPath 控件并向其添加新功能。DropDownSiteMapPath 控件在当前节点后添加一个 DropDownList,使得定位到当前页的子节点页面变得容易。此示例演示如何在创建项后使用 SiteMapNodeItem 对象,包括检查它们的 SiteMapNodeItemType 及调用 OnItemCreated 方法。

ContractedBlock.gif ExpandedBlockStart.gif 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. The
// 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 the
// 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 
= new HyperLink();

            
// 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 to
            
// 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);
        }
        
else {
            
base.InitializeItem(item);
        }
    }
    
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 the
            
// 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 
+= new EventHandler(this.DropDownNavPathEventHandler);

            
// Add a ListItem to the DropDownList for every node in the
            
// SiteMapNodes collection.
            foreach (SiteMapNode node in childNodes) {
                ddList.Items.Add(
new ListItem(node.Title, node.Url));
            }

            item.Controls.Add(ddList);
        }
    }

    
// 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);
    }
}

 

 

Web.sitemap和Web.config中的Role节点:

1、只有角色为2的用户才能看到这个节点 
<siteMapNode title="Parent1_Child1" url="~/Parent1/Parent1_Child1.aspx" roles="2"/>

2、只有角色为1或2的用户才能操作这个文件夹中的文件

 <location path="Parent2">
  <system.web>
   <authorization>
    <allow roles="1,2" />
    <deny users="*" />
   </authorization>
  </system.web>
 </location>

比如,当会员登陆后,他的个人界面有一个菜单,菜单中有所有可以看到的功能,但是有些功能他必须付费后才能使用,那么就必须设置roles属性了。

转载于:https://www.cnblogs.com/min10/archive/2008/11/19/1336567.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值