几乎每个网站里,为了方便用户在网站中进行页面导航,都少不了使用页面导航控件。有了页面导航的功能,用户可以很方便地在一个复杂的网站中进行页面之间的跳转。在以往的web编程中,要写一个好的页面导航功能,并不是那么容易的,也要使用一些技巧。而在asp.net 2.0中,为了方便进行页面导航,新增了一个叫做页面导航控件sitemapdatasource,其中还可以绑定到不同的其他页面控件,比如treeview,menu等,十分灵活,使到能很方便地实现页面导航的不同形式,而且还提供了运行时的编程接口,可以以编程的形式动态实现页面导航控件。本文将简单以几个例子来介绍一下在asp.net 2.0中如何实现页面导航。
页面导航的结构和sitemapdatasource控件
在asp.net 2.0中,要实现页面导航,应该先以xml的形式,提供出整个网站的页面结构层次。我们可以编写一个叫web.sitemap的xml文本文件,在该文件中定义出整个要导航页面的结构层次。举例如下:
<?xml version=”1.0″ encoding=”utf-8″ ?> <sitemap> <sitemapnode title=”default” description=”home” url=”default.aspx” > <sitemapnode title=”members” description=”members” url=”members.aspx”> <sitemapnode title=”my account” description=”my account” url=”myaccount.aspx” /> <sitemapnode title=”products” description=”products” url=”products.aspx” /> </sitemapnode> <sitemapnode title=”administration” description=”administration” url=”~/admin/default.aspx”> <sitemapnode title=”customer” description=”customer admin” url=”~/admin/customer/default.aspx” /> <sitemapnode title=”products admin” description=”products admin” url=”~/admin/productsadmin.aspx” /> </sitemapnode> </sitemapnode> </sitemap> |
我们可以看到,其中,web.sitemap文件必须包含根结点sitemap。而且,设置一个父sitemapnode结点,该结点表明是默认的站点首页,在该父sitemapnode结点下,可以有若干个子sitemapnode结点,分别按层次结构代表了网站的各子栏目(留意一下上例中,各个子结点之间的包含关系)。而每一个sitemapnode结点中,有如下若干个属性:
1)url属性:该属性指出要导航的栏目的地址链接,在web.sitemap中定义中,必须是每个栏目的相对地址。
2)title属性:该属性指出每个子栏目的名称,显示在页面中。
3)description属性:该属性指定时,则用户在鼠标移动到该栏目时,出现有关该栏目的相关提示,类似于tooltips属性。
在设计好sitemap属性后,接下来就可以一步步构建页面导航功能了,主要有两个步骤:
1) 向页面中添加sitemapdatasource控件。该控件会自动感应绑定web.sitemap中的内容。
2) 将sitemapdatasource控件绑定到如sitemappath,treeview,menu等控件中,也就是说,将它们的数据源设置为该sitemapdatasource控件。
知道了方法后,我们下面就分别以treeview,menu,sitemappath三种控件为例子,介绍一下如何和sitemapdatasource控件进行配合使用。
先来介绍使用treeview控件和sitemapdatasource 控件配合使用的方法。treeview树形列表控件十分适合于用来做页面导航,为了能具体说明,我们充分利用asp.net中的masterpage控件,先搭建出一个网站的基本框架架构。
在visual web developer 2005 beta 1中,新建一个网站,之后添加上文的web.sitemap文件,再添加一个名叫navigation的master类型的页面,代码如下:
<%@ master language=”c#” %> <html xmlns=”http://www.w3.org/1999/xhtml” > <head id=”head1″ runat=”server”> <title>master page</title> </head> <body> <form id=”form1″ runat=”server”> <div> <table style=”width: 100%; height: 100%” border=”1″> <tr> <td style=”width: 10%”> <asp:treeview id=”treeview1″ runat=”server” datasourceid=”sitemapdatasource1″ expanddepth=”2″ showexpandcollapse=”false” nodeindent=”10″> <levelstyles> <asp:treenodestyle font-bold=”true” font-underline=”false”/> <asp:treenodestyle font-italic=”true” font-underline=”false” /> <asp:treenodestyle font-size=”x-small” imageurl=”bullet.gif” font-underline=”false” /> </levelstyles> <nodestyle childnodespadding=”10″ /> </asp:treeview> </td> <td style=”width: 100px”> <asp:contentplaceholder id=”contentplaceholder1″ runat=”server”> </asp:contentplaceholder> </td> </tr> </table> <asp:sitemapdatasource id=”sitemapdatasource1″ runat=”server”/> </div> </form> </body> </html> |
在上面的代码中,其中的treeview控件中的datasoruce属性中,就指定了sitemapdatasource控件,并且在treeview控件中,也定义了不同结点的样式。
在完成了masterpage页面后,就等于已经把网站的模版页建立起来了,接下来就可以新建其他子页面,以继承masterpage页面,并且新建各自页面的内容了。比如,新建一个default.aspx页面,代码如下:
<%@ page language=”c#” masterpagefile=”navigation.master” title=”default page”%> <asp:content contentplaceholderid=”contentplaceholder1″ id=”content1″ runat=”server”> this is the default page </asp:content> |
可以看到,当建立了模版页后,就可以新建其他的子页面了,只需要在其中的contentplaceholderid中写入不同的内容就可以了。运行起来后,效果如图:
接下来,我们来介绍如何将menu菜单控件和sitemapdatasource 控件配合使用。其中,我们在上面的例子的基础上,在<table style=”width: 100%; height: 100%” border=”1″>下面增加如下代码就可以了,
<tr height=”100px”> <td colspan=”2″ align=”left”> <asp:menu id=”menu1″ runat=”server” datasourceid=”sitemapdatasource1″> </asp:menu> </td> </tr> |
其中,我们增加了一个menu控件,其中将其datasourceid属性设定为sitemapdatasource1就可以了,运行如下图,当然,我们可以改变menu控件的显示位置,如可以将其改成垂直样式显示。
而对于我们经常见到的显示出页面当前路径的导航条功能,在asp.net 2.0中也可以轻易实现,我们可以使用其中的sitemappath控件。我们紧接着在上文代码中的menu控件下,增加如下代码:
<tr height=”100px”> <td colspan=”2″ align=”left”> currently selected page is: <asp:sitemappath runat=”server” id=”sitemappath1″></asp:sitemappath> </td> </tr> |
要注意的是,只要增加sitemappath控件就可以了,因为它会自动和已经增加的sitemapdatasource控件进行绑定的。我们为了说明问题,另外增加一个页面member.aspx,代码如下:
<%@ page language=”c#” masterpagefile=”navigation.master” title=”members page”%> <asp:content contentplaceholderid=”contentplaceholder1″ id=”content1″ runat=”server”> this is the members page </asp:content> |
运行结果如下:
最后,我们看一下,如何通过编程的方式来获取页面导航中的相关数据。其中,必须引用到的是sitemap类,该类提供了很多相关的方法和属性,最重要的是currentnode属性,它可以指出当前用户正在浏览的是哪一个栏目页面,这用来跟踪用户在网站中的行动轨迹,并进行站点数据统计,有时是很有用的,举例如下:
<%@ page language=”c#” masterpagefile=”navigation.master” title=”members page”%> <script runat=”server”> void page_load(object sender, eventargs e) { response.write(“the currently selected root node is: ” + sitemap.currentnode.description + “<br>”); response.write(“the parent for the currently selected node is : ” + sitemap.currentnode.parentnode.description); } </script> <asp:content contentplaceholderid=”contentplaceholder1″ id=”content1″ runat=”server”> this is the members page </asp:content> |
在这个例子中,使用程序的方式,得出了用户当前正在浏览的栏目页面,以及该栏目的父栏目的名称,运行结果如下图:
可以看出,在asp.net 2.0中,实现页面导航的功能十分灵活而且方便,功能十分强大,更多的功能请参考msdn。