首页 > 开发 > Xml > 正文

使用xml作为数据源,让asp:Menu类自由扩展菜单项

2020-09-19 18:00:40
字体:
来源:转载
供稿:网友
注册会员,创建你的web开发资料库,

  先新建一个名为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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表