我在Windows窗体应用程序中创建了一个函数,允许用户在ListView中查看文件夹结构(TreeForm)和文件夹中的文件。现在,我需要为WebForm应用程序创建相同的功能。我试图使用相同的代码,但发现asp.net webform控件不包含与winform相同的属性。下面是代码的一部分,我无法确定如何转换,以便它可以在webform页面上使用。现在有人如何转换以下代码,以便它可以与asp.net webform一起使用?非常感谢任何帮助。
protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{
ListView1.Items.Clear();
ListViewItem.ListViewSubItem[] subItems;
List<string> permittedFoldersFiles = new List<string>();
if (permittedFoldersFiles.Contains(dir.Name))
{
item = new ListViewItem(dir.Name, 0);
subItems = new ListViewItem.ListViewSubItem[]
{new ListViewItem.ListViewSubItem(item, "Directory"),
new ListViewItem.ListViewSubItem(item,
dir.LastAccessTime.ToShortDateString())};
item.SubItems.AddRange(subItems);
listView1.Items.Add(item);
}
}
答案 0 :(得分:1)
尝试使用此示例,它的工作非常好
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DirectoryInfo rootInfo = new DirectoryInfo(Server.MapPath("~/MyFolder/"));
this.PopulateTreeView(rootInfo, null);
}
}
private void PopulateTreeView(DirectoryInfo dirInfo, TreeNode treeNode)
{
foreach (DirectoryInfo directory in dirInfo.GetDirectories())
{
TreeNode directoryNode = new TreeNode
{
Text = directory.Name,
Value = directory.FullName
};
if (treeNode == null)
{
//If Root Node, add to TreeView.
TreeView1.Nodes.Add(directoryNode);
}
else
{
//If Child Node, add to Parent Node.
treeNode.ChildNodes.Add(directoryNode);
}
//Get all files in the Directory.
foreach (FileInfo file in directory.GetFiles())
{
//Add each file as Child Node.
TreeNode fileNode = new TreeNode
{
Text = file.Name,
Value = file.FullName,
Target = "_blank",
NavigateUrl = (new Uri(Server.MapPath("~/"))).MakeRelativeUri(new Uri(file.FullName)).ToString()
};
directoryNode.ChildNodes.Add(fileNode);
}
PopulateTreeView(directory, directoryNode);
}
}