Program Design of the Web tree structure on the. NET platform

Source: Internet
Author: User
Li honggen
My previous article "Application of tree structure in development" is mainly implemented in Windows form. The following describes the implementation in web form. </P> <p> overview </P> <p> Treeview is an important control, whether in VB. net, C #, VB, Delphi, and other languages all act as navigators. In practice, in many cases, you need to connect the Treeview to the database to fill in its nodes. In Windows form and web form, we can use Treeview to display the tree structure, such as displaying the directory tree, displaying regions, and displaying products by category. 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 for software developers. <Br/> tree structure display methods <br/> tree structure display generally involves three methods: <br/> 1. the Treeview control is directly filled in the Treeview designer or code During interface design. <Br/> 2. Create a tree structure from the XML file. <Br/> 3. obtain data from the database and create a tree structure. <Br/> the first method is the simplest. This method is mainly used for applications with tree structures that do not change, and a tree is fixed during design. Of course, the tree structure is fixed during the design. To modify, add, or delete a tree node in the future, you must modify the source program. All are not conducive to expansion. <Br/> the second method is to extract from an XML file. Because XML itself is a tree structure, the Document Object Model Dom provided by Microsoft can easily read, operate, and modify XML documents. In. net, the application of the system. xml class can easily load XML files into the Treeview control. Microsoft's msdn also provides an example, which is not mentioned here. <Br/> the third method is to obtain data in a tree structure from the database. Generally, most of our applications are database-based. In this way, it is convenient to add, modify, and delete a node in a tree. You only need to operate the data in the database. In addition, this method can be associated, queried, and summarized with other tables in the database. by designing a view or stored procedure, you can easily query the desired data. Next, we will mainly discuss the design and implementation of this method. </P> <p> Database Design </P> <p> first, create a table tbtree in SQL Server 2000. The table structure is designed as follows: <br/> column name data type description Length Primary Key <br/> ID int node number 4 is <br/> parentid int parent node number 4 <br/> context nvarchar what we want to display node content 50 </P> <p> script for creating a table in SQL Server 2000: <br/>
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 to the table:

Set identity_insert tbtree oninsert 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 province ', 1) insert tbtree (ID, context, parentid) values (6, 'guangzhou', 5) insert tbtree (ID, context, parentid) values (7, 'sichuan province ', 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 key', 6) insert tbtree (ID, context, parentid) values (15, 'shantou ', 5) set identity_insert tbtree off

</P> <p> http://msdn.microsoft.com/downloads/samples/internet/ASP_DOT_NET_ServerControls/WebControls/default.asp </P> <p> after installation, add the Treeview to the Toolbox through the custom toolbox->. NET Framework component. <Br/> Create a new project, select the Visual Basic. Net project Asp.net web application, and drag a Treeview control on the page. </P> <p> HTML page: <br/><% @ 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>

Background code:

C # version:

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 of webform1 // public class webform1: system. web. UI. page {protected m Icrosoft. web. UI. webcontrols. treeview treeview1; private void page_load (Object sender, system. eventargs e) {// define the database connection sqlconnection Cn = new sqlconnection (); try {// initialize the connection string 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 "); // redirect the public error handling page of the program} finally {CN. close () ;}// call the recursive function to generate the addtree (0, (treenode) null) for the tree structure );} // recursively Add the public void addtree (INT parentid, treenode pnode) {dataset DS = (Dataset) This. viewstate ["ds"]; dataview dvtree = new dataview (Ds. tables [0]); // filter the parentid to obtain all the current Child sections. Point dvtree. rowfilter = "[parentid] =" + parentid; foreach (datarowview row in dvtree) {treenode node = new treenode (); If (pnode = NULL) {// Add the root node. TEXT = row ["context"]. tostring (); treeview1.nodes. add (node); node. expanded = true; addtree (int32.parse (row ["ID"]. tostring (), node); // re-recursion} else {// Add the subnode node of the current node to worker. TEXT = row ["context"]. tostring (); pnode. nodes. add (node); node. expanded = true; addtr EE (int32.parse (row ["ID"]. tostring (), node); // re-recursion }}# region web form designer generated code override protected void oninit (eventargs E) {/// codegen this call is ASP.. NET web form designer. // Initializecomponent (); base. oninit (E );} /// <summary> /// the designer supports the required methods-do not use the code editor to modify /// the content of this method /// </Summary> private void initializecomponent () {This. load + = new system. eventhandler (this. page_load) ;}# endregion }}

Postscript: Ask the reader to modify the connection string settings in the program. <Br/> Appendix: Microsoft msdn documentation, including creating a tree structure from XML in VB6 and. Net </P> <p> http://support.microsoft.com/default.aspx? Kbid = 311318 </P> <p> http://support.microsoft.com/default.aspx? Kbid = 308063 </P> <p> http://support.microsoft.com/default.aspx? Kbid = 317597 </P> <p> http://support.microsoft.com/default.aspx? Kbid = 244954 </P> <p>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.