天天看點

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

在asp.net2.0中,沒有專門的頁面導航控件,但可以使用sitemapdatasource配和datalist來實作。

sitemapdatasource控件中,需要特别的建立一個web.sitemap的xml檔案,該檔案中存貯網站的結構,

比如

<?xmlversion="1.0"encoding="utf-8"?>

<sitemapxmlns="http://schemas.microsoft.com/aspnet/sitemap-file-1.0">

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

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

<sitemapnodeurl="default3.aspx?id=1"title="社群"/>

</sitemapnode>

</sitemap>

之後,在default.aspx中,寫入代碼:

<%@pagelanguage="c#"%>

<scriptrunat=server>

protectedvoidpage_load()

{

intindex=-1;

int32.tryparse(request.querystring["id"],outindex);

tabs.selectedindex=index;

}

</script>

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

<headid="head1"runat="server">

<title>untitledpage</title>

<style>

a

color:#000000;

text-decoration:none;

.mytab

background:#6666ff;

padding:4px;

.mytabselected

background:#ff00ff;

</style>

</head>

<body>

<formid="form1"runat="server">

<div>

<table>

<asp:datalistrepeatdirection=horizontalid="tabs"runat="server"datasourceid="sitemapdatasource1">

<itemtemplate>

<tdwidth="4"height="20"valign="top"nowrapclass="mytab">

<ahref='<%#eval("url")%>'><%#eval("title")%></a>

</td>

</itemtemplate>

<selecteditemtemplate>

<tdwidth="4"height="20"valign="top"nowrapclass="mytabselected">

</selecteditemtemplate>

</asp:datalist>

</table>

<asp:sitemapdatasourceshowstartingnode=falseid="sitemapdatasource1"runat="server"/>

</div>

</form>

</body>

</html>

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