简体   繁体   English

如何在aspx页面上显示可点击目录而不动态添加控件作为子文件夹

[英]How to display clickable directories on an aspx page without dynamically adding controls as subfolders

I created an aspx website which shows a directory with its content from a FTP-Server.我创建了一个 aspx 网站,它显示了一个目录,其中包含来自 FTP 服务器的内容。 If I click a folder inside this directory, it should display the content of the sub directory and so on.如果我点击这个目录中的一个文件夹,它应该显示子目录的内容等等。

What I have tried so far is to display the content of a directory as dynamically createt WebControls.Button.到目前为止,我所尝试的是将目录的内容显示为动态创建的 WebControls.Button。 Those buttons have a onclick event difined in the c# code behind.这些按钮在后面的 c# 代码中定义了一个 onclick 事件。

This is a bad idea because of the following reason:这是一个坏主意,原因如下:

Creating new dynamically controls after a postback (clicking on a folder) prevents any onclick event , added to the sub directory buttons, to work.在回发(单击文件夹)后创建新的动态控件可防止添加到子目录按钮的任何 onclick 事件起作用。 This is, because controls need an unique ID that eventhandling works properly.这是因为控件需要一个唯一的 ID,事件处理才能正常工作。

Let me explain this with an example:让我用一个例子来解释这一点:

  • First page load -> every directory displayed gets an ID (1-7) and an onclick event assigned.第一页加载 -> 显示的每个目录都会获得一个 ID (1-7) 并分配一个 onclick 事件。

Scenario 1:场景一:

  • Opening folder "Europe" with the ID 1 -> onclick event gets fired -> the sub directories will be loaded in codebehind, displayed and get an ID (1- ..) and an onclick event assigned.打开带有 ID 1 的文件夹“Europe” -> onclick 事件被触发 -> 子目录将在代码隐藏中加载,显示并获得 ID (1- ..) 和分配的 onclick 事件。

  • Opening the subfolder "Andorra" with ID 1 -> onclick event gets fired -> But now the parent folder "Europe" will be opened, because it already had ID 1.打开 ID 为 1 的子文件夹“Andorra” -> onclick 事件被触发 -> 但现在父文件夹“Europe”将被打开,因为它已经有 ID 1。

Scenario 2场景二

  • Opening folder "Europe" with the ID 1 -> onclick event gets fired -> the sub directories will be displayed, but now we assign new IDs (6-..) and the onclick event.打开 ID 为 1 的文件夹“Europe” -> onclick 事件被触发 -> 将显示子目录,但现在我们分配新的 ID (6-..) 和 onclick 事件。

  • Opening the folder "Andorra" with the ID 6 -> onclick event does not get fired (event mechanism seems to be confused about the new ID) -> instead all the added buttons (directories) dissapear from the page.使用 ID 6 打开文件夹“Andorra”-> onclick 事件不会被触发(事件机制似乎对新 ID 感到困惑)-> 而是所有添加的按钮(目录)从页面上消失。

Additional Info: To know which button is clicked, I save the path in the Button.CommandArgument parameter, so the codebehind knows which directory to load from the FTP.附加信息:要知道单击了哪个按钮,我将路径保存在 Button.CommandArgument 参数中,以便代码隐藏知道从 FTP 加载哪个目录。

I really have no more ideas how to realize a simple clickable directory in asp.net.我真的没有更多想法如何在 asp.net 中实现一个简单的可点击目录。 I hope you understant the struggle and may have an idea to share :)我希望你能理解这场斗争,并可能有想法分享:)

Ok, here is what you do.好的,这就是你要做的。 Lets drag a button on a blank web form.让我们在空白 Web 表单上拖动一个按钮。 Next, drag and drop in a treeview.接下来,在树视图中拖放。

On the treeview control, select this option: (auto format)在树视图控件上,选择此选项:(自动格式)

在此处输入图片说明

Now, you don't have to do the above auto format.现在,您不必执行上述自动格式化。 All it does is "set up" the icons for a treeview.它所做的只是“设置”树视图的图标。 The built in list has typical bullets or > etc. And these are "just" settings in the property sheet for you.内置列表具有典型的项目符号或 > 等。这些是属性表中的“只是”设置。 (you can use your own icons, but this sets them up for you). (您可以使用自己的图标,但这会为您设置它们)。

Ok, so far, zero code.好的,到目前为止,零代码。

Now, for our code behind the button to fill the treeview.现在,为我们的按钮后面的代码填充树视图。

Dim myFiles As List(Of String) = GetAllFtpFiles(fRoot, UserId, Passwrod)
Call PopulateTreeViewFiles(fRoot, myFiles, "", Nothing)

The routine GetAllFtpFiles simple returns a list of files at a given starting folder.例程 GetAllFtpFiles 简单地返回给定起始文件夹中的文件列表。 So fRoot would be所以 fRoot 将是

"ftp://ftp.mywebsite/startingFolder/"  (we do assume trailing /)

We then call PopulateTreeViewFiles.然后我们调用 PopulateTreeViewFiles。 All it does add the list of base files.它所做的只是添加了基本文件列表。 Now we COULD populate the WHOLE tree, but that can take a bit of time, so we only populate folders when clicked on.现在我们可以填充整个树,但这可能需要一些时间,所以我们只在点击时填充文件夹。

So, the GetAllFtpFiles simple returns a list of files.因此,GetAllFtpFiles 简单返回一个文件列表。 I am sure you ALREADY have your own FTP file get/grab/fetch routine, but here is the one I used:我相信你已经有了自己的 FTP 文件获取/抓取/获取例程,但这是我使用的一个:

Private Function GetAllFtpFiles(ByVal ParentFolderpath As String, UserId As String, Password As String) As List(Of String)
    Try
        Dim ftpRequest As FtpWebRequest = CType(WebRequest.Create(ParentFolderpath), FtpWebRequest)
        ftpRequest.Credentials = New NetworkCredential(UserId, Password)
        ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory
        Dim response As FtpWebResponse = CType(ftpRequest.GetResponse(), FtpWebResponse)
        Dim streamReader As StreamReader = New StreamReader(response.GetResponseStream())
        Dim directories As List(Of String) = New List(Of String)()
        Dim line As String = streamReader.ReadLine()

        While Not String.IsNullOrEmpty(line)
            directories.Add(line)
            line = streamReader.ReadLine()
        End While
        streamReader.Close()
        Return directories

    Catch ex As Exception
        Return Nothing
    End Try

 End Function

As noted, it just returns a list for a given folder.如前所述,它只返回给定文件夹的列表。

Next up, the routine to add to the treeview.接下来,添加到树视图的例程。

Sub PopulateTreeViewFiles(fRoot As String, dtParent As List(Of String), parentId As String, treeNode As TreeNode)

    For Each sFile As String In dtParent
        Dim sFileOnly As String = Replace(sFile, parentId, "")
        Dim child As New TreeNode() With {
         .Text = Replace(sFileOnly, "/", ""),
         .Value = fRoot + sFileOnly
        }
        If InStr(sFileOnly, ".") <> 0 Then
            child.ShowCheckBox() = True         ' is a file - no expand
        Else
            child.PopulateOnDemand = True       ' is folder - allow expand
        End If
        If parentId = "" Then
            TreeView1.Nodes.Add(child)          ' empty tree - add to base tree
        Else
            treeNode.ChildNodes.Add(child)      ' user clicking on a node
        End If
    Next

End Sub

And that's it!!就是这样!!

The resulting screen looks like this:生成的屏幕如下所示:

在此处输入图片说明

So now you have a treeview based on FTP.所以现在你有一个基于 FTP 的树视图。

Note that each file does have a check box, so you can select as many files as you want, and then use the selected collection of the tree view.请注意,每个文件都有一个复选框,因此您可以根据需要选择任意数量的文件,然后使用树视图的选定集合。

So, not a lot of code, but the treeview is "dynamic".所以,代码不多,但树视图是“动态的”。 Note that we could fill the WHOLE treeview at the start (just move the ondemand code routine to the main routine), but as noted, this can slow things down a lot.请注意,我们可以在开始时填充整个树视图(只需将按需代码例程移至主例程),但如前所述,这会大大减慢速度。 This way, we ONLY pull + populate the starting folder, so it should work quite fast.这样,我们只拉 + 填充起始文件夹,所以它应该工作得非常快。

I don't have this in c#, but the above is simple enough You could send the above routines though a vb to c# converter, but the code is not complex, and is quite "basic".我在 c# 中没有这个,但是上面已经足够简单了您可以通过 vb 到 c# 转换器发送上述例程,但是代码并不复杂,并且非常“基本”。 And the FTP code could be perhaps replaced with your own.并且 FTP 代码也许可以替换为您自己的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM