2014-08-27 79 views
0

我有一個下拉菜單,它通過拾取文件夾中的文件然後在下拉列表中顯示給最終用戶。下一個可用的項目DDL VB.NET

但是,如果其中一個文件被刪除或移動,代碼就會中斷,因爲DDL選擇的文件不在那裏。

強制回傳似乎並沒有解決這個問題我試着實現IF/Else函數,但可以得到的代碼工作,如果沒有發現,然後找到下一個存在。

任何幫助將不勝感激。

下面是我使用的代碼:我假設你指的是我的答覆這裏

Private Sub RefreshDLL() 
    Dim currentSelected As String = DDL.SelectedValue 

     DDL.DataSource = IO.Directory.GetFiles(FolderName, "*.txt").Select(Function(f) IO.Path.GetFileName(f)).ToList 
     DDL.DataBind() 
     DDL.SelectedValue = currentSelected 
End Sub 

回答

0

Dynamically Add Text Files to DDL in ASP & VB

這是很容易檢測該文件是否仍然可用或刪除。但是,您還必須將TextBox放入UpdatePanel中,以便可以在其內部的數據不再有效時進行更新。如果可能的話,你可以把它放在同一個UpdatePanel中,或者把它放在一個單獨的UpdatePanel中,並使用UpdateMode="Conditional"。將UpdateMode設置爲Conditional將僅在您想更新TextBox時更新該TextBox,從而減少閃爍。

<asp:UpdatePanel runat="server" ID="UpdatePanel2" UpdateMode="Conditional"> 
     <ContentTemplate> 
      <asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine" Width="300" Height="250" /> 
     </ContentTemplate> 
    </asp:UpdatePanel> 

然後將代碼刷新下拉列表是這樣的:

Private Sub RefreshDropDownList() 
    Dim currentSelected As String = DropDownList1.SelectedValue 
    DropDownList1.DataSource = IO.Directory.GetFiles(FolderName, "*.csv").Select(Function(f) IO.Path.GetFileName(f)).ToList 
    DropDownList1.DataBind() 
    If IO.File.Exists(IO.Path.Combine(FolderName, currentSelected)) Then 
     DropDownList1.SelectedValue = currentSelected 
    Else 
     OpenSelectedFile() 
     UpdatePanel2.Update() 
    End If 
End Sub 
相關問題