| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1427 人关注过本帖
标题:TreeView 和 Menu 的用法
只看楼主 加入收藏
tml327
Rank: 1
等 级:新手上路
帖 子:510
专家分:0
注 册:2007-10-30
收藏
 问题点数:0 回复次数:5 
TreeView 和 Menu 的用法
TreeView和Menu都是用于导航,且与数据库绑定时的用法也极为相近,下面给出两个实例,请比较业务层的绑定方法
  " border="0" />
  1. Tree.aspx
  1. Tree.aspx
  <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Tree.aspx.cs" Inherits="NavTree"
   EnableViewState="false" %>
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.
  <html xmlns="http://www.
  <head runat="server">
   <title>LeftMenu_Tree</title>
  </head>
  <body bgcolor="#DDEDFD">
   <form id="form1" runat="server">
   <asp:LinkButton ID="lbtExpand" runat="server"  ToolTip="全部展开">
   全部展开
   </asp:LinkButton>
   <asp:LinkButton ID="lbtCollapse" runat="server"  Visible="false">
   全部收缩
   </asp:LinkButton>
   <asp:TreeView ID="tvMenu" SkinID="tvClass" runat="server" ShowLines="true" NodeWrap="false">
   </asp:TreeView>
   </form>
  </body>
  </html>
  2. Tree.aspx.cs
  using System;
  using System.Data;
  using System.Data.SqlClient;
  using System.Configuration;
  using System.Collections;
  using System.Web;
  using System.Web.Security;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using System.Web.UI.WebControls.WebParts;
  using System.Web.UI.HtmlControls;
  public partial class NavTree : System.Web.UI.Page
  {
   Sys.BLL.Navigation myBiz = new Sys.BLL.Navigation();
   private void Page_Load(object sender, System.EventArgs e)
   {
   if (!Page.IsPostBack)
   {
   BindTree();
   }
   }
   
   protected void BindTree()
   {
   //根据当前用户ID加载目录树
   myBiz.BindTree(tvMenu, Framework.Globals.CurrentUser.UserID);
   }
   
   //全部展开节点
   protected void lbtExpand_Click(object sender, EventArgs e)
   {
   tvMenu.ExpandAll();
   lbtExpand.Visible = false;
   lbtCollapse.Visible = true;
   }
   
   //全部折叠节点
   protected void lbtCollapse_Click(object sender, EventArgs e)
   {
   tvMenu.CollapseAll();
   lbtExpand.Visible = true;
   lbtCollapse.Visible = false;
   }
  }
  3. Menu.aspx
  <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Menu.aspx.cs" Inherits="NavMenu" %>
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.
  <html xmlns="http://www.
  <head runat="server">
   <title>Top</title>
  </head>
  <body>
   <form id="form1" runat="server">
   <asp:Menu ID="Menu1" runat="server" BackColor="#f1f1f1" ForeColor="Black" Font-Size="12px"
   Font-Names="宋体" BorderColor="background" BorderStyle="Solid" BorderWidth="0px"
   StaticSubMenuIndent="10px" Height="20px" Orientation="Horizontal" StaticEnableDefaultPopOutImage="False">
   <StaticMenuItemStyle Horiz VerticalPadding="2px" BorderWidth="1px"
   BorderColor="#f1f1f1" />
   <StaticHoverStyle BackColor="#cccccc" BorderColor="background" BorderStyle="Solid"
   BorderWidth="1px" />
   <DynamicHoverStyle BackColor="#C6DDFE" BorderColor="background" BorderStyle="Solid"
   BorderWidth="0px" />
   <DynamicMenuStyle BackColor="Menu" BorderStyle="Outset" BorderWidth="1px" Horiz
   VerticalPadding="0px" BorderColor="MenuBar" />
   <DynamicMenuItemStyle Horiz VerticalPadding="2px" />
   </asp:Menu>
   </form>
  </body>
  </html>
  4. Menu.aspx.cs
  using System;
  using System.Data;
  using System.Configuration;
  using System.Collections;
  using System.Web;
  using System.Web.Security;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using System.Web.UI.WebControls.WebParts;
  using System.Web.UI.HtmlControls;
  public partial class NavMenu : System.Web.UI.Page
  {
   Sys.BLL.Navigation myBiz = new Sys.BLL.Navigation();
   protected void Page_Load(object sender, EventArgs e)
   {
   if (!Page.IsPostBack)
   {
   myBiz.BindMenu(Menu1);
   }
   }
  }
  5. 业务层:App_Code/Sys.BLL.Navigation.cs
  using System;
  using System.Data;
  using System.Configuration;
  using System.Web;
  using System.Web.Security;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using System.Web.UI.WebControls.WebParts;
  using System.Web.UI.HtmlControls;
  namespace Sys.BLL
  {
   /**//// <summary>
   /// 系统导航 业务层
   /// </summary>
   public class Navigation : Framework.BLL.BizObject
   {
   变量 构造函数#region 变量 构造函数
   Sys.DAL.Navigation myData = new Sys.DAL.Navigation();
   string ImagePath = Framework.Globals.WebRoot + "Images/";
   
   /**//// <summary>
   /// 系统导航
   /// </summary>
   public Navigation()
   { }
   #endregion
   
   目录树(TreeView)#region 目录树(TreeView)
   DataTable ds_TreeView;
   /**//// <summary>
   /// 根据当前用户ID加载目录树
   /// </summary>
   public void BindTree(TreeView tvMenu, int userID)
   {
   ds_TreeView = myData.BindTree(userID).Tables[0];
   if (ds_TreeView.Rows.Count > 0)
   BindTree(tvMenu, 0, (TreeNode)null);
   }
   
   private void BindTree(TreeView tvMenu, int ParentID, TreeNode pNode)
   {
   string NodeId = "MenuID";
   string NodeName = "MenuName";
   string ParentId = "ParentID";
   string Url = "Url";
   string Icon = "Icon";
   
   DataView dvTree = new DataView(ds_TreeView);
   //过滤ParentId,得到当前的所有子节点
   dvTree.RowFilter = ParentId + " = " + ParentID;
   
   foreach (DataRowView drv in dvTree)
   {
   TreeNode Node = new TreeNode();
   
   if (pNode == null)
   { //添加根节点
   string nodename = drv[NodeName].ToString().Trim();
   string nodeid = drv[NodeId].ToString().Trim();
   
   Node.Text = nodename;
   Node.ToolTip = nodename;
   Node.Value = nodeid;
   Node.Target = "main";
   
   if (drv[Icon].ToString().Trim() == string.Empty)
   { Node.ImageUrl = ImagePath + "menu/ie.gif"; }
   else
   { Node.ImageUrl = ImagePath + "menu/" + drv[Icon].ToString().Trim(); }
   
   //导航模式 & 选择模式
   //要使一个节点处于导航模式,请将该节点的 NavigateUrl 属性值设置为空字符串 ("") 以外的值
   //若要使节点处于选择模式,请将节点的 NavigateUrl 属性设置为空字符串。
   Node.NavigateUrl = "";
   Node.SelectAction = TreeNodeSelectAction.Expand;
   Node.Expanded = false;
   Node.PopulateOnDemand = false;
   
   tvMenu.Nodes.Add(Node); //***注意区别:根节点
   BindTree(null, Int32.Parse(nodeid), Node); //再次递归
   }
   else
   { //?添加当前节点的子节点
   string nodename = drv[NodeName].ToString().Trim();
   string nodeid = drv[NodeId].ToString().Trim();
   
   Node.Text = nodename;
   Node.ToolTip = nodename;
   Node.Value = nodeid;
   Node.Target = "main";
   
   if (drv[Icon].ToString().Trim() == string.Empty)
   { Node.ImageUrl = ImagePath + "menu/ie.gif"; }//默认图标
   else
   { Node.ImageUrl = ImagePath + "menu/" + drv[Icon].ToString().Trim(); }
   
   Node.NavigateUrl = drv[Url].ToString();
   Node.SelectAction = TreeNodeSelectAction.Expand;
   Node.Expanded = false;
   Node.PopulateOnDemand = false;
   
   pNode.ChildNodes.Add(Node); //***注意区别:子节点
   BindTree(null, Int32.Parse(nodeid), Node); //再次递归
   }
   }
   }
   #endregion
   
   导航菜单(Menu)#region 导航菜单(Menu)
   DataTable ds_BindMenu;
   /**//// <summary>
   /// 加载获取导航菜单
   /// </summary>
   public void BindMenu(Menu tvMenu)
   {
   ds_BindMenu = myData.BindMenu().Tables[0];
   if (ds_BindMenu.Rows.Count > 0)
   BindMenu(tvMenu, 0, (MenuItem)null);
   }
   /**//// <summary>
   /// 加载获取导航菜单
   /// </summary>
   private void BindMenu(Menu tvMenu, int ParentID, MenuItem pNode)
   {
   DataView dvTree = new DataView(ds_BindMenu);
   dvTree.RowFilter = "ParentId =" + ParentID;//过滤ParentId,得到当前的所有子节点
   foreach (DataRowView drv in dvTree)
   {
   int id = Convert.ToInt32(drv["ID"]);
   int parentID = Convert.ToInt32(drv["ParentID"]);
   string text = drv["Text"].ToString();
   string value = drv["Value"] == DBNull.Value ? string.Empty : drv["Value"].ToString();
   string imageUrl = drv["ImageUrl"] == DBNull.Value ? string.Empty : drv["ImageUrl"].ToString();
   string navigateUrl = drv["NavigateUrl"] == DBNull.Value ? string.Empty : drv["NavigateUrl"].ToString();
   string target = drv["Target"] == DBNull.Value ? string.Empty : drv["Target"].ToString();
   
   MenuItem item = new MenuItem();
   if (pNode == null)
   { //添加根节点
   item.Text = text;
   item.Value = value;
   item.Target = target;
   
   if (imageUrl == string.Empty)
   {
   item.ImageUrl = ImagePath + "winXP/winxp_folder.gif";
   }
   else
   {
   item.ImageUrl = imageUrl;
   }
   item.NavigateUrl = navigateUrl;
   item.Selectable = true;
   
   tvMenu.Items.Add(item); //***注意区别:根节点
   BindMenu(null, id, item); //再次递归
   
   }//检索
   else
   { //?添加当前节点的子节点
   item.Text = text;
   item.Value = value;
   item.Target = target;
   
   if (imageUrl == string.Empty)
   {
   item.ImageUrl = ImagePath + "winXP/winxp_folder.gif";
   }
   else
   {
   item.ImageUrl = imageUrl;
   }
   item.NavigateUrl = navigateUrl;
   item.Selectable = true;
   
   pNode.ChildItems.Add(item); //***注意区别:子节点
   BindMenu(null, id, item); //再次递归
   }
   }
   }
   #endregion
   }//class end
  }
搜索更多相关主题的帖子: Menu TreeView 用法 数据库 
2007-11-24 16:14
wuhongyao3
Rank: 1
等 级:新手上路
帖 子:202
专家分:0
注 册:2006-4-18
收藏
得分:0 
这样的知识要顶一下了
建议大家多多发.呵呵....

2007-11-24 19:48
tml327
Rank: 1
等 级:新手上路
帖 子:510
专家分:0
注 册:2007-10-30
收藏
得分:0 
谢谢捧场呀!:)

轻狂如我,心伤谁知!
2007-11-24 20:46
FenteLi
Rank: 1
来 自:上海
等 级:新手上路
帖 子:124
专家分:0
注 册:2007-11-24
收藏
得分:0 
不错,我就喜欢发源码的人,呵呵,继续加油,我顶你的肺...
2007-11-26 00:05
MikeFT
Rank: 1
来 自:青岛
等 级:新手上路
帖 子:644
专家分:0
注 册:2006-11-1
收藏
得分:0 
谢谢楼主分享!!!

Live And Learn !!!
2007-11-26 10:05
tml327
Rank: 1
等 级:新手上路
帖 子:510
专家分:0
注 册:2007-10-30
收藏
得分:0 

轻狂如我,心伤谁知!
2007-11-26 16:20
快速回复:TreeView 和 Menu 的用法
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.087476 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved