日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

.NET平台下Web树形结构程序设计

發布時間:2023/12/13 asp.net 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 .NET平台下Web树形结构程序设计 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

.NET平臺下Web樹形結構程序設計

我的上篇文章《樹形結構在開發中的應用》主要是在Windows Form下的實現,下面是Web Form下的實現。

數據庫設計

首先,我們在SQL SERVER 2000里建立一個表tbTree,表的結構設計如下:

列名 數據類型 描述 長度 主鍵
ID Int 節點編號 4
ParentID Int 父節點編號 4 ?
ConText Nvarchar 我們要顯示的節點內容 50 ?
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=<?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" />1.0.2.226, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %><?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

<%@ 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);?????//再次遞歸
????????????????????????????}

?????????????????????}
???????????????????
??????????????}
????????????
?
??????????????
Web?Form?Designer?generated?code#region?Web?Form?Designer?generated?code
??????????????
override?protected?void?OnInit(EventArgs?e)
??????????????
{
?????????????????????
//
?????????????????????
//?CODEGEN該調用是?ASP.NET?Web?窗體設計器所必需的。
?????????????????????
//
?????????????????????InitializeComponent();
?????????????????????
base.OnInit(e);
??????????????}

??????????????
??????????????
/**////?<summary>
??????????????
///設計器支持所需的方法?-?不要使用代碼編輯器修改
??????????????
///?此方法的內容
??????????????
///?</summary>

??????????????private?void?InitializeComponent()
??????????????
{????
?????????????????????
this.Load?+=?new?System.EventHandler(this.Page_Load);
?
??????????????}

??????????????
#endregion

???????}

}

聲明:本文版權與解釋權歸李洪根所有,如需轉載,請保留完整的內容及此聲明。 QQ: 21177563?? MSN: lihonggen@hotmail.com 專欄:http://www.csdn.net/develop/author/netauthor/lihonggen0/

轉載于:https://www.cnblogs.com/coolylh/archive/2005/11/14/276027.html

總結

以上是生活随笔為你收集整理的.NET平台下Web树形结构程序设计的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。