asp.net 2.0中使用sitemapDATAsource做页面导航

在ASP.NET 2.0中,没有专门的页面导航控件,但可以使用SITEMAPdatasource配和DATALIST来实现。
SITEMAPDATASOURCE控件中,需要特别的建立一个web.sitemap的XML文件,该文件中存贮网站的结构,
比如

1<sitemap xmlns=" http://schemas.microsoft.com/AspNet/SiteMap-File-1.0 ">
2<sitemapnode title="首页" url="default.aspx?id=-1">
3<sitemapnode title="商品" url="default2.aspx?id=0"></sitemapnode>
4<sitemapnode title="社区" url="default3.aspx?id=1"></sitemapnode>
5</sitemapnode>
6</sitemap>

之后,在default.aspx中,写入代码:

1@ Page Language="C#" 
 1<script runat="server">
 2
 3protected void Page_Load() 
 4
 5{ 
 6
 7int index = -1; 
 8
 9Int32.TryParse(Request.QueryString["id"], out index); 
10
11Tabs.SelectedIndex = index; 
12
13} 
14
15</script>
 1<html xmlns=" http://www.w3.org/1999/xhtml ">
 2<head id="Head1" runat="server">
 3<title>Untitled Page</title>
 4<style>
 5
 6a 
 7
 8{ 
 9
10color: #000000; 
11
12text-decoration: none; 
13
14} 
15
16.myTab 
17
18{ 
19
20background: #6666ff; 
21
22padding: 4px; 
23
24} 
25
26.myTabSelected 
27
28{ 
29
30background: #ff00ff; 
31
32padding: 4px; 
33
34} 
35
36</style>
37</head>
38<body>
39<form id="form1" runat="server">
40<div>
41<table>
42<asp:datalist datasourceid="SiteMapDataSource1" id="Tabs" repeatdirection="Horizontal" runat="server">
43<itemtemplate>
44<td class="myTab" height="20" nowrap="" valign="top" width="4">
45<a href='```
46# Eval("Url") 
47```'>```
48# Eval("Title") 
49```</a>
50</td>
51</itemtemplate>
52<selecteditemtemplate>
53<td class="myTabSelected" height="20" nowrap="" valign="top" width="4">
54<a href='```
55# Eval("Url") 
56```'>```
57# Eval("Title") 
58```</a>
59</td>
60</selecteditemtemplate>
61</asp:datalist>
62</table>
63<asp:sitemapdatasource id="SiteMapDataSource1" runat="server" showstartingnode="false"></asp:sitemapdatasource>
64</div>
65</form>
66</body>
67</html>

就可以实现简单的页面导航的效果了

Published At
Categories with Web编程
Tagged with
comments powered by Disqus