Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Control Refresh() Method
Message
Information générale
Forum:
ASP.NET
Catégorie:
Formulaires
Divers
Thread ID:
00921607
Message ID:
00921974
Vues:
24
>I'm not sure if listboxes can be sorted

Kevin,

I just wanted to pass along, by default web form list objects do not supply a sort property. Below is a VB version of a routine that can be used to sort both the list box and Drop Down List. Thought that might be handy later down the road.
    ' This routine is used to sort a given list object
    Public Shared Sub SortList(ByRef oLst As System.Web.UI.WebControls.ListControl, Optional ByVal bDesc As Boolean = False)

        ' Variables
        Dim aLst As New ArrayList()
        Dim aVal As New ArrayList()
        Dim nC%, nS%

        ' Assign Value
        nS = IIf(bDesc, -1, 1)

        ' Load Items into List
        For nC = 0 To oLst.Items.Count - 1
            aLst.Add(oLst.Items(nC).Text)
        Next

        ' Sort List
        aLst.Sort()

        ' Load Values for Items
        For nC = 0 To aLst.Count - 1
            aVal.Add(oLst.Items.FindByText(aLst(nC)).Value)
        Next

        ' Clear List
        oLst.Items.Clear()

        ' Reload Values
        If nS > 0 Then
            For nC = 0 To aLst.Count - 1 Step nS
                oLst.Items.Add(New System.Web.UI.WebControls.ListItem(aLst(nC), aVal(nC)))
            Next
        Else
            For nC = aLst.Count - 1 To 0 Step nS
                oLst.Items.Add(New System.Web.UI.WebControls.ListItem(aLst(nC), aVal(nC)))
            Next
        End If

        ' Free Memory
        aLst.Clear()
        aVal.Clear()
        aLst = Nothing
        aVal = Nothing

    End Sub
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform