asp.net 2.0中使用sitemapDATAsource做頁面導航

來源:互聯網
上載者:User

在ASP.NET 2.0中,沒有專門的頁面導航控制項,但可以使用SITEMAPdatasource配和DATALIST來實現。
SITEMAPDATASOURCE控制項中,需要特別的建立一個web.sitemap的XML檔案,該檔案中存貯網站的結構,
比如
<?xml version="1.0" encoding="utf-8" ?>

<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >

    <siteMapNode url="default.aspx?id=-1" title="首頁">

            <siteMapNode url="default2.aspx?id=0" title="商品"/>

            <siteMapNode url="default3.aspx?id=1" title="社區"/>

    </siteMapNode>

</siteMap>
之後,在default.aspx中,寫入代碼:
<%@ Page Language="C#" %>

 

<script runat=server>

 

    protected void Page_Load()

    {

        int index = -1;

        Int32.TryParse(Request.QueryString["id"], out index);

 

        Tabs.SelectedIndex = index;

    }

       

</script>

 

<html xmlns="http://www.w3.org/1999/xhtml" >

<head id="Head1" runat="server">

    <title>Untitled Page</title>

    <style>

        a

        {

            color: #000000;

            text-decoration: none;

        }

       

        .myTab

        {

            background: #6666ff;

            padding: 4px;

        }

 

        .myTabSelected

        {

            background: #ff00ff;

            padding: 4px;

        }

    </style>

 

</head>

<body>

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

    <div>

       

        <table>

        <asp:DataList RepeatDirection=Horizontal ID="Tabs" runat="server" DataSourceID="SiteMapDataSource1">

            <ItemTemplate>

                 <td width="4" height="20" valign="top" nowrap class="myTab">

                   <a href='<%# Eval("Url") %>'><%# Eval("Title") %></a>

                </td>

            </ItemTemplate>

           <SelectedItemTemplate>

                <td width="4" height="20" valign="top" nowrap class="myTabSelected">

                   <a href='<%# Eval("Url") %>'><%# Eval("Title") %></a>

                </td>

           </SelectedItemTemplate>

        </asp:DataList>

        </table>

        <asp:SiteMapDataSource ShowStartingNode=false ID="SiteMapDataSource1" runat="server" />

    </div>

    </form>

</body>

</html>

就可以實現簡單的頁面導航的效果了


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.