我有一个带有GridView控件的页面。
在Gridview中有一个TemplateField,它包含一个ListBox控件:
<asp:TemplateField HeaderText="Uploaded Files">
<ItemTemplate>
<asp:ListBox ID="ListBoxFiles" runat="server"></asp:ListBox>
</ItemTemplate>
</asp:TemplateField>
当页面加载时,我需要使用服务器上文件夹中的文件列表填充这个ListBox。我不知道该怎么做。
我可以用标签达到类似的效果:
<asp:TemplateField HeaderText="Uploaded Files">
<ItemTemplate>
<asp:ListBox ID="ListBoxFiles" runat="server"></asp:ListBox>
<asp:Label ID="LabelFiles" runat="server" Text='<%#GetFiles(Eval("DocDescription")) %>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
后面有以下代码
Public Function GetFiles(param As String)
GetFiles = ""
Try
Dim filePaths() As String = Directory.GetFiles(Server.MapPath("~/Uploads/") & Session("LastFirst") & " - " & Session("StudentUID") & "/" & param & "/")
For Each filePath As String In filePaths
GetFiles = GetFiles & "<br/>" & Path.GetFileName(filePath)
Next
GetFiles = Right(GetFiles, Len(GetFiles) - 5)
Catch
End Try
End Function
但是我希望用户能够选择要删除的文件。
当页面加载时,如何填充ListBox?
发布于 2015-11-13 15:41:28
您可以这样做(注意:从C#转换而来):
Protected Sub Page_Load(sender As Object, e As EventArgs)
For Each Row As GridViewRow In GridView1.Rows
If Row.RowType = DataControlRowType.DataRow Then
Dim ListBoxFiles As ListBox = TryCast(Row.FindControl("ListBoxFiles"), ListBox)
ListBoxFiles.Items.Add("aaa")
ListBoxFiles.Items.Add("bbb")
ListBoxFiles.Items.Add("ccc")
End If
Next
End Sub
https://stackoverflow.com/questions/33702858
复制