先新建一個名為menu的xml文件,內容如下:
<?xml version="1.0" encoding="utf-8" ?>
<menu>
<menuitem url="~/default.aspx" title="首頁" description=""/>
<menuitem url="~/news.aspx" title="新聞" description="">
<menuitem url="~/news.aspx" title="國內新聞" description=""/>
<menuitem url="~/news.aspx" title="國際新聞" description=""/>
</menuitem>
</menu>
然后在頁面上拖放一個menu控件,并拖放一個xmldatasource控件,id為xmldatasource1,將menu的 datasourceid設置為xmldatasource1。
配置xmldatasource控件,使datafile="~/menu.xml" xpath="menu/menuitem",這里采用xpath來選取數據,即將menuitem的數據篩選出來。
此時,再綁定menu控件的數據,告訴menu控件該怎么顯示從xml文件篩選出來的數據。選中menu控件,在屬性窗口選取databindings,在彈出的對話框里,在左上角的列表中選中menuitem,單擊添加按鈕將menuitem添加到左下的列表中,再在此列表中選擇menuitem,此是右邊的列表將顯示要綁定的屬性,使navigateurlfield="url" textfield="title"。按確定按鈕返回設計界面,此時就會顯示出"首頁"和"新聞"兩個菜單項。
設計html代碼如下:
<%@ page language="c#" autoeventwireup="true" codebehind="default.aspx.cs" inherits="coaffiliate._default" %>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>untitled page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:menu id="menu1" runat="server" datasourceid="xmldatasource1" orientation="horizontal" staticenabledefaultpopoutimage="false">
<databindings>
<asp:menuitembinding datamember="menuitem" navigateurlfield="url" textfield="title" />
</databindings>
</asp:menu>
<asp:xmldatasource id="xmldatasource1" runat="server" datafile="~/menu.xml" xpath="menu/menuitem"></asp:xmldatasource>
</div>
</form>
</body>
</html>
http://www.cnblogs.com/ofei/archive/2007/01/20/625745.html
新聞熱點
疑難解答