扩展了一下。每个控件都有其独特的写法和用法,treeview就是这样的一个控件。做个类似资源管理器的页面。如果逐层遍历,代码会不断重复,所以这时需要用递归算法
Code
public static void TreeBindMethod(TreeView tvFile,string target)
{
tvFile.Nodes.Clear();
TreeNode rootNode = new TreeNode("文件管理", "0", "~/Images/file/folder.gif","FileMain.aspx?id=0","");
tvFile.Nodes.Add(rootNode);
IList<FileInfo> list = FileInfoService.GetAllEntities().Where(c=>c.IsDelete==false).ToList();
var query = list.Where(c => c.ParentId == 0).Select(c => c).ToList();
foreach (FileInfo file in query)
{
TreeNode node = GetFileTree(out target, file);
AddChildTree(list, file, target, node);
AddTreeMethod(rootNode, node);
}
}
private static string filename;
private static TreeNode GetFileTree(out string target, FileInfo file)
{
TreeNode node = new TreeNode();
if (file.FileName.Length > 8)
{
filename = file.FileName.Substring(0, 7) + "";
}
else {
filename = file.FileName;
}
node.Text = filename;
node.Value = file.Id.ToString();
node.ImageUrl = file.FileType.FileTypeImage;
node.NavigateUrl = string.Format("FileMain.aspx?Id={0}",file.Id);
target = file.FilePath;
return node;
}
private static void AddChildTree(IList<FileInfo> list, FileInfo file,string target, TreeNode fatherNode)
{
int id = file.Id;
var childquery = list.Where(c => c.ParentId == id).ToList();
foreach (FileInfo childfile in childquery)
{
TreeNode childnode = new TreeNode();
childnode.Text = childfile.FileName;
childnode.ImageUrl = childfile.FileType.FileTypeImage;
childnode.NavigateUrl = string.Format("FileMain.aspx?Id={0}", childfile.Id);
target = childfile.FilePath;
//这里调用了自己,就是递归算法,遍历了树所有节点
AddChildTree(list, childfile, target, childnode);
AddTreeMethod(fatherNode, childnode);
}
}
private static void AddTreeMethod(TreeNode fatherNode, TreeNode childnode)
{
fatherNode.ChildNodes.Add(childnode);
}
//当然,可以设置一个int depth=0;的参数,在递归算法前,if(depth++>4 yield break;来设置递归深度
public static void TreeBindMethod(TreeView tvFile,string target)
{
tvFile.Nodes.Clear();
TreeNode rootNode = new TreeNode("文件管理", "0", "~/Images/file/folder.gif","FileMain.aspx?id=0","");
tvFile.Nodes.Add(rootNode);
IList<FileInfo> list = FileInfoService.GetAllEntities().Where(c=>c.IsDelete==false).ToList();
var query = list.Where(c => c.ParentId == 0).Select(c => c).ToList();
foreach (FileInfo file in query)
{
TreeNode node = GetFileTree(out target, file);
AddChildTree(list, file, target, node);
AddTreeMethod(rootNode, node);
}
}
private static string filename;
private static TreeNode GetFileTree(out string target, FileInfo file)
{
TreeNode node = new TreeNode();
if (file.FileName.Length > 8)
{
filename = file.FileName.Substring(0, 7) + "";
}
else {
filename = file.FileName;
}
node.Text = filename;
node.Value = file.Id.ToString();
node.ImageUrl = file.FileType.FileTypeImage;
node.NavigateUrl = string.Format("FileMain.aspx?Id={0}",file.Id);
target = file.FilePath;
return node;
}
private static void AddChildTree(IList<FileInfo> list, FileInfo file,string target, TreeNode fatherNode)
{
int id = file.Id;
var childquery = list.Where(c => c.ParentId == id).ToList();
foreach (FileInfo childfile in childquery)
{
TreeNode childnode = new TreeNode();
childnode.Text = childfile.FileName;
childnode.ImageUrl = childfile.FileType.FileTypeImage;
childnode.NavigateUrl = string.Format("FileMain.aspx?Id={0}", childfile.Id);
target = childfile.FilePath;
//这里调用了自己,就是递归算法,遍历了树所有节点
AddChildTree(list, childfile, target, childnode);
AddTreeMethod(fatherNode, childnode);
}
}
private static void AddTreeMethod(TreeNode fatherNode, TreeNode childnode)
{
fatherNode.ChildNodes.Add(childnode);
}
//当然,可以设置一个int depth=0;的参数,在递归算法前,if(depth++>4 yield break;来设置递归深度