扩展了一下。每个控件都有其独特的写法和用法,treeview就是这样的一个控件。做个类似资源管理器的页面。如果逐层遍历,代码会不断重复,所以这时需要用递归算法
Code
publicstaticvoidTreeBindMethod(TreeViewtvFile,stringtarget)
{
tvFile.Nodes.Clear();
TreeNoderootNode=newTreeNode("文件管理","0","~/Images/file/folder.gif","FileMain.aspx?id=0","");
tvFile.Nodes.Add(rootNode);
IList<FileInfo>list=FileInfoService.GetAllEntities().Where(c=>c.IsDelete==false).ToList();
varquery=list.Where(c=>c.ParentId==0).Select(c=>c).ToList();
foreach(FileInfofileinquery)
{
TreeNodenode=GetFileTree(outtarget,file);
AddChildTree(list,file,target,node);
AddTreeMethod(rootNode,node);
}
}
privatestaticstringfilename;
privatestaticTreeNodeGetFileTree(outstringtarget,FileInfofile)
{
TreeNodenode=newTreeNode();
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;
returnnode;
}
privatestaticvoidAddChildTree(IList<FileInfo>list,FileInfofile,stringtarget,TreeNodefatherNode)
{
intid=file.Id;
varchildquery=list.Where(c=>c.ParentId==id).ToList();
foreach(FileInfochildfileinchildquery)
{
TreeNodechildnode=newTreeNode();
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);
}
}
privatestaticvoidAddTreeMethod(TreeNodefatherNode,TreeNodechildnode)
{
fatherNode.ChildNodes.Add(childnode);
}
//当然,可以设置一个intdepth=0;的参数,在递归算法前,if(depth++>4yieldbreak;来设置递归深度
优质内容筛选与推荐>>
1、12、Spring Boot 2.x 集成 MongoDB2、驾校考试秘笈 不用看书就能通过!!3、day 22 面向对象 类与对象 名称空间操作4、你是“细”精你就赢了 游戏开发者怎样做好玩家细分5、leetcode -- 56.合并区间
长按二维码向我转账
受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。