Recursively load the dynamic loading TreeView Server Control (original)

Source: Internet
Author: User

 

Below are the database tables

Drag a TreeView control in the design view

 

Default. aspx. cs Code

/*
* Created by: du Chenggang
* Creation Time: 22:05:30
* Note:
* All Rights Reserved: Xia chufeng duchenggang.accp@163.com.
*/
Using System;
Using System. Data;
Using System. Configuration;
Using System. Collections;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Web. UI. HtmlControls;
Using System. Collections. Generic;
Using System. Data. SqlClient;

Public partial class _ Default: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
If (! IsPostBack)
{
List <FileInfo> listFileInfo = GetFileInfo ();
For (int I = 0; I <listFileInfo. Count; I ++)
{
TreeNode fatherNode = new TreeNode ();
If (listFileInfo [I]. ParentId = 0)
{
FatherNode. Value = listFileInfo [I]. FileId. ToString ();
FatherNode. Text = listFileInfo [I]. FileName. ToString ();
FatherNode = GetTreeNode (fatherNode, listFileInfo );
This. TreeView1.Nodes. Add (fatherNode );
}
}
}
}

Public static TreeNode GetTreeNode (TreeNode fatherNode, List <FileInfo> listFileInfo)
{
For (int I = 0; I <listFileInfo. Count; I ++)
{
TreeNode childNode = new TreeNode ();
If (listFileInfo [I]. ParentId. ToString () = fatherNode. Value)
{
ChildNode. Value = listFileInfo [I]. FileId. ToString ();
ChildNode. Text = listFileInfo [I]. FileName. ToString ();
ChildNode = GetTreeNode (childNode, listFileInfo );
FatherNode. ChildNodes. Add (childNode );
}

}
Return fatherNode;
}

Public static List <FileInfo> GetFileInfo ()
{
String SQL = "SELECT * FROM FileInfo ";
String connString = "Data Source =.; Initial Catalog = MyOffice; User Id = sa; PassWord = sa ;";
List <FileInfo> listFileInfo = new List <FileInfo> ();
SqlConnection con = new SqlConnection (connString );
Con. Open ();
SqlCommand cmd = new SqlCommand (SQL, con );
SqlDataReader reader = cmd. ExecuteReader ();
If (reader. HasRows)
{
While (reader. Read ())
{
FileInfo fileInfo = new FileInfo ();
FileInfo. FileId = (int) reader ["FileId"];
FileInfo. FileName = (string) reader ["FileName"];
FileInfo. FileType = (int) reader ["FileType"];
FileInfo. Remark = (string) reader ["Remark"];
FileInfo. FileOwner = (string) reader ["FileOwner"];
FileInfo. CreateDate = (DateTime) reader ["CreateDate"];
FileInfo. ParentId = (int) reader ["ParentId"];
FileInfo. FilePath = (string) reader ["FilePath"];
FileInfo. IfDelete = (int) reader ["IfDelete"];
ListFileInfo. Add (fileInfo );
}
Reader. Close ();
}
Con. Close ();
Return listFileInfo;

}
}

FileInfo. cs entity class ....

/*
* Created by: du Chenggang
* Creation Time: 22:05:30
* Note:
* All Rights Reserved: Xia chufeng duchenggang.accp@163.com.
*/
Using System;
Using System. Data;
Using System. Configuration;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Web. UI. HtmlControls;
Using System. Data. SqlClient;
/// <Summary>
/// Summary of FileInfo
/// </Summary>

Public class FileInfo
{

Private int fileId;
Private string fileName = String. Empty;
Private int fileType;
Private string remark = String. Empty;
Private string fileOwner = String. Empty;
Private DateTime createDate;
Private int parentId;
Private string filePath = String. Empty;
Private int ifDelete;

Public FileInfo (){}

Public int FileId
{
Get {return this. fileId ;}
Set {this. fileId = value ;}
}

Public string FileName
{
Get {return this. fileName ;}
Set {this. fileName = value ;}
}

Public int FileType
{
Get {return this. fileType ;}
Set {this. fileType = value ;}
}

Public string Remark
{
Get {return this. remark ;}
Set {this. remark = value ;}
}

Public string FileOwner
{
Get {return this. fileOwner ;}
Set {this. fileOwner = value ;}
}

Public DateTime CreateDate
{
Get {return this. createDate ;}
Set {this. createDate = value ;}
}

Public int ParentId
{
Get {return this. parentId ;}
Set {this. parentId = value ;}
}

Public string FilePath
{
Get {return this. filePath ;}
Set {this. filePath = value ;}
}

Public int IfDelete
{
Get {return this. ifDelete ;}
Set {this. ifDelete = value ;}
}

}

 

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.