ASP.NET-在页面加载时加载LIstBox控件
我有一个带有GridView控件的页面 在Gridview中有一个包含ListBox控件的TemplateField:ASP.NET-在页面加载时加载LIstBox控件,asp.net,vb.net,listbox,Asp.net,Vb.net,Listbox,我有一个带有GridView控件的页面 在Gridview中有一个包含ListBox控件的TemplateField: <asp:TemplateField HeaderText="Uploaded Files"> <ItemTemplate> <asp:ListBox ID="ListBoxFiles" runat="server"></asp:ListBox> </ItemTemplate> <
<asp:TemplateField HeaderText="Uploaded Files">
<ItemTemplate>
<asp:ListBox ID="ListBoxFiles" runat="server"></asp:ListBox>
</ItemTemplate>
</asp:TemplateField>
当页面加载时,我需要用服务器上文件夹中的文件列表填充此列表框。我不知道该怎么做
我可以通过标签实现类似的效果:
<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
公共函数GetFiles(参数为字符串)
GetFiles=“”
尝试
Dim filepath()格式为String=Directory.GetFiles(Server.MapPath(“~/Uploads/”)&Session(“LastFirst”)&Session(“StudentUID”)&“¶m&/”)
将每个文件路径作为文件路径中的字符串
GetFiles=GetFiles&“
”和Path.GetFileName(filePath)
下一个
GetFiles=Right(GetFiles,Len(GetFiles)-5)
抓住
结束尝试
端函数
但是我希望用户能够选择要删除的文件
加载页面时如何填充列表框?您可以这样做(注意:从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