This is a migrated thread and some comments may be shown as answers.

Get the index number of the listbox item?

2 Answers 559 Views
ListView
This is a migrated thread and some comments may be shown as answers.
Kevin
Top achievements
Rank 1
Kevin asked on 12 Jul 2011, 01:15 PM
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 Answers, 1 is accepted

Sort by
0
Dimitar Terziev
Telerik team
answered on 15 Jul 2011, 09:22 AM
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!

0
Kevin
Top achievements
Rank 1
answered on 15 Jul 2011, 04:47 PM
Thanks Dimitar  - that worked.  Your help is much appreciated.
Tags
ListView
Asked by
Kevin
Top achievements
Rank 1
Answers by
Dimitar Terziev
Telerik team
Kevin
Top achievements
Rank 1
Share this question
or