. Design of dynamic Web tree structure based on net platform

Source: Internet
Author: User
Tags table name
Web| Program | create | dynamic | design

To create a tree structure with the TreeView control

Database structure Design

This article uses ms-sqlserver2000, which involves the following two database tables:

Table name

Description

Table name

Description

Mainbords

Main Number Table

Subborads

Child tables

The fields in each table are as follows: Field settings in Mainboards

Field

Type

Description

Mainid

Integral type

Index number

Mainname

Character type

The name of the main edition

Field settings in Subboards:

Field settings in Subboards:

Field

Type

Description

SubID

Integral type

Index number

Mainid

Integral type

The corresponding main version number

SubName

Character type

Child name

Field

Type

Description

SubID

Integral type

Index number

Mainid

Integral type

The corresponding main version number

SubName

Character type

Child name

3. 2 Creating a tree structure:

3. 2 Creating a tree structure:

3. 2 Creating a tree structure:

To implement the tree structure, we first have to add the master node, which is the Mainboards table.

To connect to the database first:

SqlConnection CN = new SqlConnection ();

Initialize the connection string, modify it according to your actual situation

CN. connectionstring= "Data source=localhost;initial catalog=bbs;persist security Info=false;user Id=sa; Password=sa; ";

CN. Open ();

SqlDataAdapter ADP = new SqlDataAdapter ("select * from Mainboards", CN);

DataSet ds=new DataSet ();

Adp. Fill (DS);

Add Master Node

int Nummain=ds. Tables[0]. Rows.Count;

for (int i=0;i<nummain;i++)

{

TreeNode node=new TreeNode ();

Node.text =ds. Tables[0]. ROWS[I][1]. ToString ();

Tree. Nodes.Add (Node);

Node.expanded=true;

int Mainid=convert.toint32 (DS2. Tables[0]. Rows[i][0]. ToString ());

Calling the Addsubtree method to add a child node

Addsubtree (Mainid,node);

Addsubtree method

public void Addsubtree (int mainid,treenode pnode)

{//Read the contents of the Subborads table

SqlConnection con = new SqlConnection ();

Con. connectionstring= "Data source=localhost;initial catalog=bbs;persist security Info=false;user Id=sa; Password=sa; ";

Con. Open ();

SqlDataAdapter adp1 = new SqlDataAdapter ("select * from Subboards where mainid= '" "+mainid+" ", con);

DataSet ds1=new DataSet ();

Adp1. Fill (DS1);

int NUMSUB=DS1. Tables[0]. Rows.Count;

To add a child node under a given primary node

for (int i=0;i<numsub;i++)

{

TreeNode node=new TreeNode ();

Node.text =ds1. Tables[0]. ROWS[I][2]. ToString ();

PNODE.NODES.ADD (Node);

After you click here to set the node to redirect to the page:

Node.navigateurl= "display.aspx?id=";

Node.expanded=false;} }



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.