TreeView用于实现树型菜单

 

TreeView使用心得(一)
日期:2004年6月9日 星期三  位置:办公室  天气:晴朗  作者:china
在Asp.Net中,TreeView用于实现树型菜单最好的控件,实现方法如下:

具体方法是:创建一个数据库,设计树图信息表S_Menu,包含NodeId、ParentId、NodeName、Address、Icon字段,其它字段根据实际业务而定,节点名称NodeName将在树型控件的节点上显示,NodeId字段保存节点的唯一标识号,ParentId表示当前节点的父节点号,标识号组成了一个“链表”,记录了树上节点的结构。设计一个Web窗体其上放置TreeView控件,修改其属性Id为tvMenu。

数据结构如下:
CREATE TABLE [dbo].[S_Menu] (
 [NodeId] [char] (6) COLLATE Chinese_PRC_CI_AS NULL ,
 [ParentId] [char] (6) COLLATE Chinese_PRC_CI_AS NULL ,
 [NodeName] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
 [Address] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
 [Icon] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO


数据库如下:
NodeId ParentId NodeName Address  Icon
100000 0  公共查询部   icon_document.gif
100001 100000  人民币日报查询 public/a1.aspx icon_settings.gif
100002 100000  外币日报查询 public/a2.aspx icon_settings.gif
100003 0  分行科技部   icon_document.gif
100004 100003  人民币日报查询 tech/a1.aspx icon_settings.gif
100005 100003  外币日报查询 tech/a2.aspx icon_settings.gif
100006 0  福田支行   icon_document.gif
100007 100006  月存款进度表 a1.aspx  icon_settings.gif
100008 100006  月存款走势图 a2.aspx  icon_settings.gif
100009 0  罗湖支行   icon_document.gif
100010 100009  月存款进度表 a1.aspx  icon_settings.gif
100011 100009  月存款走势图 a2.aspx  icon_settings.gif
    
menu_left.aspx文件如下:
<%@ Register TagPrefix="iewc" Namespace="Microsoft.Web.UI.WebControls" Assembly="Microsoft.Web.UI.WebControls, Version=1.0.2.226, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Page language="c#" Codebehind="menu_Left.aspx.cs" AutoEventWireup="false" Inherits="hzquery.menu.menu_Left" %>
<HTML>
 <HEAD>
  <title>menu_Left</title>
  <meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
  <meta name="CODE_LANGUAGE" Content="C#">
  <meta name="vs_defaultClientScript" content="JavaScript">
  <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
 </HEAD>
 <body MS_POSITIONING="GridLayout">
  <form id="menu_Left" method="post" runat="server">
   <iewc:TreeView id="tvMenu" runat="server"></iewc:TreeView>
  </form>
 </body>
</HTML>


CodeBehind代码如下:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Microsoft.Web.UI.WebControls;
using System.Data.SqlClient;


namespace hzquery.menu
{
 /// <summary>
 /// menu_Left 的摘要说明。
 /// </summary>
 public class menu_Left : System.Web.UI.Page
 {
  protected Microsoft.Web.UI.WebControls.TreeView tvMenu;
  SqlConnection Conn;
  SqlDataAdapter myCmd;
  DataSet ds;
  string cmdSelect;


  private void Page_Load(object sender, System.EventArgs e)
  {
   Conn=new SqlConnection(Application["ConnString"].To

TreeView使用心得(二)
日期:2004年6月9日 星期三  位置:办公室  天气:晴朗  作者:china

很多时候,我们需要实现在客户端显示和更新TreeView中的信息,通过Javascript控制是有效的手段之一,这时我们需要将TreeView放入FrameSet的中,如下:
<frameset cols="24%,76%">
  <FRAMESET rows="58%,42%">
   <frame name="left" src="treecview.aspx">
   <FRAME name="down" src="Summary.aspx">
  </FRAMESET>
  <FRAMESET rows="79%,21%" id="rightform">
   <frame name="right" src="Control.aspx">
   <FRAME name="rightdown">
  </FRAMESET>

然后,通过javascript:
parent.frames["right"].location.href="Control.aspx?id="+ xxx;
控制frameset中right内容的显示。

如果需要通过right刷新TreeView,则可以使用
parent.frames["left"].location.href="treecview.aspx"

TreeView使用心得(三)
日期:2004年6月9日 星期三  位置:办公室  天气:晴朗  作者:china

在Treeview中实现"全部展开"和"全部折叠"的功能,Expandnodes(Me.TreeView1, true)展开全部节点,Expandnodes(Me.TreeView1, false)折叠全部节点,如下:

public void Expandnodes(Microsoft.Web.UI.WebControls.TreeNode node,bool Expand)
{
    foreach(Microsoft.Web.UI.WebControls.TreeNode n in node.Nodes)
    {
 if(n.Nodes.Count>0)
 {
     n.Expanded = Expand;
     Expandnodes(n, Expand);
 }
     }
}
public void Expandnodes(Microsoft.Web.UI.WebControls.TreeView treeview, bool Expand)
{
 foreach(Microsoft.Web.UI.WebControls.TreeNode n in treeview.Nodes)
 {
                if(n.Nodes.Count>0)
        {
  n.Expanded = Expand;
  Expandnodes(n, Expand);
        }
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值