【问题标题】:ASP.NET - Load a LIstBox Control on Page LoadASP.NET - 在页面加载时加载 LIstBox 控件
【发布时间】:2015-11-13 22:33:39
【问题描述】:

我有一个带有 GridView 控件的页面。

在 Gridview 中有一个包含 ListBox 控件的 TemplateField:

<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?

【问题讨论】:

    标签: asp.net vb.net listbox


    【解决方案1】:

    你可以这样做(注意:从 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
    

    【讨论】:

    • 这正是我所需要的。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-30
    • 2019-08-04
    • 1970-01-01
    • 2011-03-07
    • 2023-03-11
    • 1970-01-01
    相关资源
    最近更新 更多