2015-11-13 59 views
0

我有一個帶有GridView控件的頁面。ASP.NET - 在頁面加載時加載LIstBox控件

在GridView的還有包含ListBox控件的TemplateField:

<asp:TemplateField HeaderText="Uploaded Files"> 
    <ItemTemplate> 
     <asp:ListBox ID="ListBoxFiles" runat="server"></asp:ListBox> 
    </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 

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

用下面的代碼,但我希望用戶必須選擇要刪除的文件的能力。

如何在頁面加載時填充ListBox?

回答

0

你可以做這樣的(注:從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 
+0

這正是我所需要的東西。謝謝! –