Column
WEB tree structure program design under .NET platform
Li Honggen
My last article "Application of tree structure in development" is mainly implemented under Windows Form, telling below to implement the implementation under Web Form.
Overview
TreeView is an important control, either in VB.NET, C # or VB, Delphi, and other languages, it acts as a navigation role. In actual work, many cases need to connect TreeView with the database to fill its node. In Windows Form and Web Form, we can display tree structure with TreeView, such as displaying directory trees, display areas, classification display items. It can be said that in most software development, TreeView is an indispensable display control. Therefore, the design of the tree structure has become an eternal topic of software developers.
Tree structure display method
There are three ways to show a tree structure:
1. Fill the TreeView control directly in the TreeView designer or code when designing the interface.
2. Create a tree structure from the XML file.
3. Get data from the database to create a tree structure.
The first way is the easiest, this way is mainly used for applications that are generally not changed, and a tree is fixed when designing. Of course, the structure of the tree is fixed when designing, and then want to modify, increase, and delete the node of the tree, you must modify the source program. All is not intended to expand.
The second way is extracted from the XML file. Since the XML itself is a tree structure, Microsoft provides the document object model DOM convenient to read, operate, and modify the XML document. In .NET, Apply the System.xml class to easily load the XML file into the TreeView control, Microsoft's MSDN also provides an example, and there is no more.
In the third way, the data of the tree structure is obtained from the database. Generally speaking, most of our applications are based on a database. This way, increase, modify, and delete a tree is convenient, as long as the data in the database can be operated. Moreover, this approach can be associated with other tables in the database, queries and summarizes, easy to check the relevant data you want by designing views or stored procedures. Below, we mainly discuss the design and implementation of this approach.
Database Design
First, we establish a table TBTree in SQL Server 2000, the structure design of the table is as follows:
Column Name Data Type Description Length Primary Key
ID INT Node No. 4 is
ParentID Int Parent Node 4
Context nvarchar We want to display the node content 50
Construction tables in 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]
Add the following records in the table:
Set Identity_Insert Tbtree On
INSERT TBTREE (ID, Context, Parentid) Values (1, 'China ", 0)
INSERT TBTREE (ID, Context, Parentid Values (2, 'Beijing ", 11)
INSERT TBTREE (ID, Context, Parentid) Values (3, 'Tianjin ", 11)
INSERT TBTREE (ID, Context, Parentid) Values (4, 'Hebei Province ", 1)
INSERT TBTREE (ID, Context, Parentid) Values (5, 'Guangdong ", 1) INSERT TBTREE (ID, Context, Parentid) Values (6,' Guangzhou ', 5)
INSERT TBTREE (ID, Context, Parentid) Values (7, 'Sichuan ", 1)
INSERT TBTREE (ID, Context, Parentid) Values (8, 'Chengdu', 7)
INSERT TBTREE (ID, Context, Parentid) Values (9, 'Shenzhen', 5)
INSERT TBTREE (ID, Context, Parentid) VALUES (10, 'Shijiazhuang', 4)
INSERT TBTREE (ID, Context, Parentid) Values (11, 'Liaoning Province ", 1)
INSERT TBTREE (ID, Context, Parentid) Values (12, 'Dalian', 11)
INSERT TBTREE (ID, Context, Parentid) Values (13, 'Shanghai ", 1)
INSERT TBTREE (ID, Context, Parentid) Values (14, 'Tianhe Software Park ", 6)
INSERT TBTREE (ID, Context, Parentid) Values (15, 'Shantou ", 5)
Set Identity_INSERT TBTREE OFF
download link
Http://msdn.microsoft.com/downloads/sample/internet/asp_dot_net_serverControls/webControls/default.asp
After installation, the TreeView is added to the toolbox by "Custom Toolbox" -> ". Net Framework Components.
Create a new project, select the Visual Basic.Net Engineering ASP.NET web application, drag a TreeView control on the page.
HTML Page:
<% @ 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"%>%>
HEAD>