递归加载动态加载TreeView服务器控件(原创)
下面是数据库表截图
在设计视图中拖一个TreeView控件
Default.aspx.cs Code
/*
*创建人:杜成刚
*创建时间:01/19/2010 22:05:30
*说明:
*版权所有:夏楚枫 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 实体类。。。。
/*
*创建人:杜成刚
*创建时间:01/19/2010 22:05:30
*说明:
*版权所有:夏楚枫 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>
/// 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; }
}
}