2012年10月1日 星期一

網站巡覽控制項

Choosing a Method to Navigate Pages

Using the Site Map Web Server Control

Site Map Web Server Control 指的是工具列中的巡覽類別的控制項,包括 Menu 、SiteMapPath、TreeView,它們都可以拿來製作網站地圖。

要製作網站地圖,必須要有一個網站地圖資料檔,它是一個副檔名為 .sitemap 的 XML 文件檔。 它包含整個網站的階層架構,你可以任意的維護這個檔案,以調整階層架構,或者隱藏某些不想顯示的節點。

底下是一個網站地圖資料檔的範例。 地圖資料檔中的每一個節點都使用 <siteMapNode> 項目表示。

<?xml version="1.0" encoding="utf-8" ?>
<siteMap>
  <siteMapNode url="~/Index.aspx" title="首頁"  description="首頁">
    
    <siteMapNode title="Creating Custom Web Controls" url=""  description="">
      <siteMapNode title="Working with User Controls"  description="Working with ..." url="~/C10_Creating_Custom_Web_Controls/WebForm1.aspx"/>
      <siteMapNode title="Working with Custom Web Server Controls"  description="Working with ..." url="~/C10_Creating_Custom_Web_Controls/WebForm2.aspx"/>
    </siteMapNode>
    
    <siteMapNode title="Web Server Controls" url="">
      <siteMapNode title="Web Server Controls" url="~/C02_Web_Server_Controls/WebForm1.aspx"></siteMapNode>
      <siteMapNode title="Common Web Server Controls" url="~/C02_Web_Server_Controls/WebForm2.aspx"></siteMapNode>
      <siteMapNode title="Specialized Web Server Controls" url="~/C02_Web_Server_Controls/WebForm3.aspx"></siteMapNode>
    </siteMapNode>
    
    <siteMapNode title="Input Validation and SiteNavigation" url="">
      <siteMapNode title="Input Validation" url="~/C03_Input_Validation_and_Site_Navigation/WebForm1.aspx"></siteMapNode>
      <siteMapNode title="Site Navigation " url="~/C03_Input_Validation_and_Site_Navigation/WebForm2.aspx"></siteMapNode>
      <siteMapNode title="SiteMapPath " url="~/C03_Input_Validation_and_Site_Navigation/SiteMapPath1.aspx"></siteMapNode>
    </siteMapNode>
    
  </siteMapNode>
</siteMap>

Using the SiteMap Class

The SiteMap class provides programmatic access to the site navigation hierarchy from within your code-behind page. Its two primary properties are RootNode and CurrentNode, and both return SiteMapNode instances.

SiteMap 類別的屬性

SiteMapNode 類別的屬性

SiteMapNode currentNode = SiteMap.CurrentNode;
SiteMapNode rootNode = SiteMap.RootNode;

myDebug.ResponseBR("RootNode Url = {0}", rootNode.Url);
myDebug.ResponseBR("CurrentNode Url = {0}", currentNode.Url);
myDebug.ResponseBR("CurrentNode Title = {0}", currentNode.Title);
myDebug.ResponseBR("CurrentNode Description = {0}", currentNode.Description);

//RootNode Url = /Index.aspx
//CurrentNode Url = /C03_Input_Validation_and_Site_Navigation/SiteMapPath1.aspx
//CurrentNode Title = SiteMapPath 
//CurrentNode Description = 

Displaying Site Map Information to Users

The site map information is just an XML file. To display this information, you need to put a navigational control on a Web page. These controls can take a site map file or a SiteMapDataSource control as their data source and display information accordingly.

A SiteMapDataSource control is simply a control designed to provide you with programmatic access to a site map file. This control can also be used by the navigation controls to provide a source for their data. You can use a SiteMapDataSource control by dragging it onto your page. It will automatically connect with the site map file you have defined for your site.

SiteMapDataSource 屬性

The SiteMapDataSource does not have a visual representation to a user. Instead, it manages access to your site map data. To show the data to a user you must add a navigational control and connect it to the SiteMapDataSource control. There are three main navigational controls available in ASP.NET :MenuTreeViewSiteMapPath .

The SiteMapPath Control automatically picks up the site map file; you do not need to configure a data source.

<form id="form1" runat="server">
<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" />
<div>
    <h1>SiteMapPath</h1>
    <asp:SiteMapPath ID="SiteMapPath1" runat="server">
    </asp:SiteMapPath>

    <h1>TreeView</h1>
    <asp:TreeView ID="TreeView1" runat="server" 
        DataSourceID="SiteMapDataSource1">
    </asp:TreeView>

    <h1>Menu</h1>
    <asp:Menu ID="Menu1" runat="server" 
        DataSourceID="SiteMapDataSource1" 
        Orientation="Vertical" 
        MaximumDynamicDisplayLevels="5" >
    </asp:Menu>
</div>
</form>

沒有留言:

張貼留言