IEWebControls TreeView Sample

來源:互聯網
上載者:User
資料庫設計首先,我們在SQL SERVER 2000裡建立一個表tbTree,表的結構設計如下:
ID    int    節點編號  KEY
ParentID  int  父節點編號
Context  nvarchar  名稱

在SQL SERVER 2000中建表的指令碼:

CREATE TABLE [dbo].[tbTree] (

       [ID] [int] IDENTITY (1, 1) NOT NULL ,

       [Context] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,

       [ParentID] [int] NULL

) ON [PRIMARY]

 在表中添加如下記錄:

SET IDENTITY_INSERT tbtree ON

insert tbtree (ID,Context,ParentID)  values ( 1,'中國',0)

insert tbtree (ID,Context,ParentID)  values ( 2,'北京',1)

insert tbtree (ID,Context,ParentID)  values ( 3,'天津',1)

insert tbtree (ID,Context,ParentID)  values ( 4,'河北省',1)

insert tbtree (ID,Context,ParentID)  values ( 5,'廣東省',1)

insert tbtree (ID,Context,ParentID)  values ( 6,'廣州',5)

insert tbtree (ID,Context,ParentID)  values ( 7,'四川省',1)

insert tbtree (ID,Context,ParentID)  values ( 8,'成都',7)

insert tbtree (ID,Context,ParentID)  values ( 9,'深圳',5)

insert tbtree (ID,Context,ParentID)  values ( 10,'石家莊',4)

insert tbtree (ID,Context,ParentID)  values ( 11,'遼寧省',1)

insert tbtree (ID,Context,ParentID)  values ( 12,'大連',11)

insert tbtree (ID,Context,ParentID)  values ( 13,'上海',1)

insert tbtree (ID,Context,ParentID)  values ( 14,'天河軟體園',6)

insert tbtree (ID,Context,ParentID)  values ( 15,'汕頭',5)

SET IDENTITY_INSERT tbtree off

下載Treeview控制項地址http://msdn.microsoft.com/downloads/samples/internet/ASP_DOT_NET_ServerControls/WebControls/default.asp安裝後,通過“自訂工具箱”->“.net架構組件”把TreeView添加到工具箱裡。建立一個項目,選擇Visual Basic.Net 工程Asp.net Web應用程式,在頁面上拖畫一個TreeView控制項。  Html頁:

<%@ Register TagPrefix="iewc" Namespace="Microsoft.Web.UI.WebControls" Assembly="Microsoft.Web.UI.WebControls, Version=1.0.2.226, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm1.aspx.vb" Inherits="Tree.WebForm1"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<HTML>

     <HEAD>

         <title>WebForm1</title>

         <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.0">

         <meta name="CODE_LANGUAGE" content="Visual Basic 7.0">

         <meta name="vs_defaultClientScript" content="javascript">

         <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">

     </HEAD>

     <body MS_POSITIONING="GridLayout">

         <form id="Form1" method="post" runat="server">

              <FONT face="宋體">

                   <iewc:TreeView id="TreeView1" style="Z-INDEX: 101; LEFT: 39px; TOP: 68px" runat="server"></iewc:TreeView></FONT>

         </form>

     </body>

</HTML>

  後台代碼:

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 TreeCS{       /// <summary>       /// WebForm1 的摘要說明       /// </summary>       public class WebForm1 : System.Web.UI.Page       {              protected Microsoft.Web.UI.WebControls.TreeView TreeView1;                     private void Page_Load(object sender, System.EventArgs e)              {                     // 定義資料庫連接                     SqlConnection CN = new SqlConnection();                     try                      {                            //初始化連接字串                            CN.ConnectionString= "data source=pmserver;initial catalog=Benchmark;persist security info=False;user id=sa;Password=sa;";                            CN.Open();                             SqlDataAdapter adp = new SqlDataAdapter("select * from tbTree",CN);                            DataSet ds=new DataSet();                            adp.Fill(ds);                            this.ViewState["ds"]=ds;                      }                     catch (Exception ex)                     {                            Session["Error"] = ex.ToString();                            Response.Redirect("error.aspx");       //̀跳轉程式的公用錯誤處理頁面                     }                     finally                      {                            CN.Close();                     }                     //調用遞迴函式,完成樹形結構的產生                     AddTree(0, (TreeNode)null);              }               //遞迴添加樹的節點              public void AddTree(int ParentID,TreeNode pNode)               {                     DataSet ds=(DataSet) this.ViewState["ds"];                      DataView dvTree = new DataView(ds.Tables[0]);                     //過濾ParentID,得到當前的所有子節點                     dvTree.RowFilter =  "[PARENTID] = " + ParentID;                      foreach(DataRowView Row in dvTree)                      {                            TreeNode Node=new TreeNode() ;                            if(pNode == null)                             {    //添加根節點                                   Node.Text = Row["ConText"].ToString();                                   TreeView1.Nodes.Add(Node);                                   Node.Expanded=true;                                   AddTree(Int32.Parse(Row["ID"].ToString()), Node);    //再次遞迴                            }                             else                             {   //̀添加當前節點的子節點                                   Node.Text = Row["ConText"].ToString();                                   pNode.Nodes.Add(Node);                                   Node.Expanded = true;                                   AddTree(Int32.Parse(Row["ID"].ToString()),Node);     //再次遞迴                            }                     }                                 }                           #region Web Form Designer generated code              override protected void OnInit(EventArgs e)              {                     //                     // CODEGEN該調用是 ASP.NET Web Form設計器所必需的。                     //                     InitializeComponent();                     base.OnInit(e);              }                            /// <summary>              ///設計器支援所需的方法 - 不要使用代碼編輯器修改              /// 此方法的內容              /// </summary>              private void InitializeComponent()              {                         this.Load += new System.EventHandler(this.Page_Load);               }              #endregion       }} 後記:請讀者自行修改程式中的連接字串設定。

聯繫我們

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

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

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.