用TreeView實現樹菜單

來源:互聯網
上載者:User
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"].ToString());
   CreateDataSet();
   InitTree(tvMenu.Nodes,"0");
  }
  //建立資料集
  private DataSet CreateDataSet()
  {
   cmdSelect="select * from S_Menu";
   myCmd=new SqlDataAdapter(cmdSelect,Conn);
   ds=new DataSet();
   myCmd.Fill(ds,"tree");
   return ds;
  }
  //建樹的基本思路是:從根節點開始遞迴調用顯示子樹
         private void InitTree(TreeNodeCollection Nds,string parentId)
  {
   DataView dv=new DataView();
   TreeNode tmpNd;
   string intId;
   dv.Table=ds.Tables["tree"];
   dv.RowFilter="ParentId="" + parentId + """ ;
   foreach(DataRowView drv in dv)
   {
    tmpNd=new TreeNode();
    tmpNd.ID=drv["NodeId"].ToString();
    tmpNd.Text=drv["NodeName"].ToString();
    tmpNd.ImageUrl="../images/"+drv["Icon"].ToString();
    tmpNd.NavigateUrl="../"+drv["Address"].ToString();
    Nds.Add(tmpNd);
    intId=drv["ParentId"].ToString();
    InitTree(tmpNd.Nodes,tmpNd.ID);
   }
  }



  #region Web Form Designer generated code
  override protected void OnInit(EventArgs e)
  {
   InitializeComponent();
   base.OnInit(e);
  }
  private void InitializeComponent()
  {    
   this.Load += new System.EventHandler(this.Page_Load);



  }
  #endregion
}
}

相關文章

Beyond APAC's No.1 Cloud

19.6% IaaS Market Share in Asia Pacific - Gartner IT Service report, 2018

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。