国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

ASP.NET - SiteMapPath Web 服務器控件概述

2019-11-17 03:58:48
字體:
來源:轉載
供稿:網友
 SiteMapPath 控件包含來自站點地圖的導航數據。此數據包括有關網站中的頁的信息,如 URL、標題、說明和導航層次結構中的位置。若將導航數據存儲在一個地方,則可以更方便地在網站的導航菜單中添加和刪除項。



下面的代碼示例演示包含在 Web.sitemap 文件中的站點地圖數據。

<siteMap>

  <siteMapNode title="Home" description="Home" url="~/default.aspx" >

    <siteMapNode title="Services" description="Services we offer"

      url="~/Services.aspx">

    <siteMapNode title="Training" description="Training classes"

      url="~/Training.aspx" />

    <siteMapNode title="Consulting" description="Consulting services"

      url="~/Consulting.aspx" />

    </siteMapNode>

  </siteMapNode>

</siteMap>

      說明: 只有在站點地圖中列出的頁才能在 SiteMapPath 控件中顯示導航數據。如果將 SiteMapPath 控件放置在站點地圖中未列出的頁上,該控件將不會向客戶端顯示任何信息。



通過添加下面的代碼,可以向 Training.aspx 頁添加 SiteMapPath 控件。

<asp:SiteMapPath ID="SiteMapPath1" Runat="server"></asp:SiteMapPath>



      當在瀏覽器中查看 Training.aspx 頁時,SiteMapPath 控件將顯示如下類似信息,并以超鏈接的形式呈現“主頁”和“服務”:

        主頁 > 服務 > 培訓

使用 SiteMapPath 控件無需代碼和綁定數據就能創建站點導航。此控件可自動讀取和呈現站點地圖信息。但如果需要,您也可以使用 SiteMapPath 控件來更改站點地圖數據。有關更多信息,請參見如何:以編程方式修改內存中的站點地圖節點。

SiteMapPath 控件使用戶能夠向后導航 -- 從當前頁到站點層次結構中更高層的頁。但是,SiteMapPath 控件不讓用戶向前導航 -- 從當前頁到站點層次結構中較低層的頁。例如,可以在新聞組或者留言板應用程序中使用 SiteMapPath 控件,使用戶能查看當前瀏覽的文章的路徑。



一、SiteMapPath 類

      SiteMapPath 控件是一種站點導航控件,反映 SiteMap 對象提供的數據。它提供了一種用于輕松定位站點的節省空間方式,用作當前顯示頁在站點中位置的引用點。此種類型的控件通常稱為面包屑或眉毛,因為它顯示了超鏈接頁名稱的分層路徑,從而提供了從當前位置沿頁層次結構向上的跳轉。SiteMapDataSource。SiteMapPath 對于分層頁結構較深的站點很有用,在此類站點中 TreeView 或 Menu 可能需要較多的頁空間。

SiteMapPath 控件直接使用網站的站點地圖數據。如果將其用在未在站點地圖中表示的頁面上,則其不會顯示。有關站點地圖的更多信息,請參見 asp.net 站點導航概述。

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 方法。



示例

下面的代碼示例在 Web 窗體頁中以聲明方式使用了 SiteMapPath 控件。此示例演示一些優先級規則,這些規則控制了將模板和樣式應用到 SiteMapPath 節點的順序。

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">



<script runat="server">

</script>



<html  >

    <head runat="server">

    <title>ASP.NET Example</title>

</head>

<body>

        <form id="form1" runat="server">



            <!-- The following example demonstrates some of the orders

                 of precedence when applying styles and templates to

                 functional nodes of a SiteMapPath.



                 The NodeStyle and RootNodeStyle define the same attributes,

                 but are different and conflict with each other: the

                 RootNodeStyle supersedes NodeStyle, and is the style

                 rendered. Notice, however, that the underline style

                 defined by NodeStyle is still applied.



                 Both a CurrentNodeStyle and a CurrentNodeTemplate are

                 defined. A template supersedes a style for a node

                 type, so CurrentNodeTemplate is displayed and CurrentNodeStyle

                 is ignored. -->



            <asp:SiteMapPath ID="SiteMapPath1" runat="server"

                RenderCurrentNodeAsLink="true"

                NodeStyle-Font-Names="Franklin Gothic Medium"

                NodeStyle-Font-Underline="true"

                NodeStyle-Font-Bold="true"

                RootNodeStyle-Font-Names="Symbol"

                RootNodeStyle-Font-Bold="false"

                CurrentNodeStyle-Font-Names="Verdana"

                CurrentNodeStyle-Font-Size="10pt"

                CurrentNodeStyle-Font-Bold="true"

                CurrentNodeStyle-ForeColor="red"

                CurrentNodeStyle-Font-Underline="false">

                <CURRENTNODETEMPLATE>

                        <asp:Image id="Image1" runat="server" ImageUrl="WebForm2.jpg" AlternateText="WebForm2"/>

                </CURRENTNODETEMPLATE>

            </asp:SiteMapPath>





        </form>

    </body>

</html>



上一個示例使用了默認的站點地圖提供程序,以及具有如下結構的 Web.sitemap 文件。

<siteMap>

  <siteMapNode title="WebForm1" description="WebForm1" url="WebForm1.aspx" >

    <siteMapNode title="WebForm2" description="WebForm2" url="WebForm2.aspx"/>

  </siteMapNode>

</siteMap>



      下面的代碼示例演示如何通過重寫 InitializeItem 方法,擴展 SiteMapPath 控件并向其添加新功能。DropDownSiteMapPath 控件在當前節點后添加一個 DropDownList,使得定位到當前頁的子節點頁面變得容易。此示例演示如何在創建項后使用 SiteMapNodeItem 對象,包括檢查它們的 SiteMapNodeItemType 及調用 OnItemCreated 方法。

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

    }

}





本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/dodream/archive/2009/12/18/5029754.aspx
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 莎车县| 宝兴县| 三亚市| 海丰县| 徐汇区| 长海县| 二手房| 剑川县| 应用必备| 腾冲县| 抚宁县| 巩留县| 柳河县| 明光市| 诸暨市| 西盟| 开原市| 阿坝| 泌阳县| 延川县| 云霄县| 内黄县| 丹巴县| 天全县| 酉阳| 五指山市| 忻城县| 当雄县| 溆浦县| 土默特右旗| 竹北市| 三门县| 贡觉县| 临漳县| 高要市| 加查县| 徐州市| 黄平县| 云林县| 闻喜县| 安岳县|