Get the index number of the listbox item?

3 posts, 0 answers
  1. Kevin
    Kevin avatar
    16 posts
    Member since:
    Jul 2011

    Posted 12 Jul 2011 Link to this post

    Hi

    I am updating an old project to use some of the Telerik controls - I am new to your controls.  A standard ASP.NET Listbox was previously used in a modal popup to re-order items in a list  - once the user was happy with the new order of items they click a 'Save Order' button to save the new order of items and close the popup. The function that works fine and does this is:
     
    Protected Sub SaveOrder(ByVal sender As Object, ByVal e As EventArgs)
     
           Dim intID As Integer
           Dim intIndex As Integer
             Dim strUpdate As String
           Dim cmdUpdate As SqlCommand
     
           connTC.Open()
     
           For Each item As RadListBoxItem In ListBox1.Items 
     
               'get the ID of the item
               intID = item.Value
     
               'get the index number in the listbox
               intIndex = ListBox1.Items.IndexOf(item)
     
               'increase by 1 since listbox index starts at 0
               intIndex = intIndex + 1
     
               strUpdate = "Update tblGalleryImages SET fldOrder=@fldOrder WHERE fldGalleryImageID=@fldGalleryImageID"
     
               cmdUpdate = New SqlCommand(strUpdate, connTC)
               cmdUpdate.Parameters.AddWithValue("@fldOrder", intIndex)
               cmdUpdate.Parameters.AddWithValue("@fldGalleryImageID", intID)
     
               cmdUpdate.ExecuteNonQuery()
     
           Next
     
     
           connTC.Close()
     
     
           'hide the modal popup
           ModalTextPopup.Hide()
     
     
     
       End Sub



    I have tried applying the same function to the RadListBox (ID =ListBox1) .  I have made the following change on one of the lines:

    " For Each item As RadListBoxItem In ListBox1.Items"

    However there is an error on the following line:

    intIndex = ListBox1.Items.IndexOf(item)

    I can not get the index of the Radlistbox item.  The error is:

    'Telerik.Web.UI.ControlItemCollection.Protected Friend Overridable Function IndexOf(item As Telerik.Web.UI.ControlItem) As Integer' is not accessible in this context because it is 'Protected Friend'.

    Any help in getting the index of the Radlistbox item would be appreciated.  The 'save order' must be done with an 'external' button as the user must be happy with the display order etc. before they save it - in other words I can not use Autopostback etc.

    Thanks in advance.

    Kevin



  2. Dimitar Terziev
    Admin
    Dimitar Terziev avatar
    1266 posts

    Posted 15 Jul 2011 Link to this post

    Hi Kevin,

    The reason for the experienced behavior is due to the fact that you are trying to get the index of the items in the wrong way. Please try the following implementation:
    For Each item As RadListBoxItem In RadListBox1.Items
        intID = (Int16.Parse(item.Value))
     
        intValue = RadListBox1.FindItemIndexByValue(item.Value)
     
     
        intValue = intValue + 1
    Next


    All the best,
    Dimitar Terziev
    the Telerik team

    Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Kevin
    Kevin avatar
    16 posts
    Member since:
    Jul 2011

    Posted 15 Jul 2011 Link to this post

    Thanks Dimitar  - that worked.  Your help is much appreciated.
Back to Top