简体   繁体   中英

Next Available item DDL VB.NET

I've got a drop down that works by picking up files in a folder to then display in the dropdown list to the end user.

However if one of the files is deleted or moved, the code breaks if it's mid-way through because the DDL is selecting a file that isn't there.

Forcing postback doesn't seem to solve this issue I tried implementing IF/Else function but could get the code to work for if nothing found then find next that exists.

Any help would be greatly appreciated.

Below is the code I'm using:

    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

I am assuming you are referring to my reply here: Dynamically Add Text Files to DDL in ASP & VB

It is easy to detect whether the file is still available or deleted. But you will also have to put the TextBox inside an UpdatePanel so that it can be updated, when the data inside it is no longer valid. You can put that in the same UpdatePanel if possible, or put it in a separate UpdatePanel with UpdateMode="Conditional" . Setting UpdateMode to Conditional will update that TextBox only when you want to update it, thus reducing flickering.

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

And then the code to refresh the DropdownList goes like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM