Link to home
Start Free TrialLog in
Avatar of bham3dman
bham3dman

asked on

How do I find the index of a selected item in a listview control

Greetings experts,

I'm a little new to this and don't yet fully understand the listview control, but here's what I'm trying to do:

I have a listview control with which I'm using templates (a couple of labels and an image button) in an image gallery.  The datapager for the listview has a page size of 15 and there are several pages of items.  When a user clicks an image button, I need to determine the index of that selected item within the listview collection so that I may, at some later point, return the listview control to the page that contains that item.  For instance, a user clicks an image button (thumbnail image) and the page navigates away to show a full-sized image.  When the user returns to the page, I want to reset the listview to the page the user was on before navigating away.

I tried to capture the selected index with:

dim idx as Integer
 idx = ListView1.SelectedIndex

However, this returns a value of -1 regardless of the item selected.  Please help this dumb guy figure this out. :)
Avatar of klakkas
klakkas

You could keep the Index of each imagebutton in the tag field, save it when the user clicks and use it when you return to this page.



Further, if the image button does not result in a postback, you could store the index in the Query String.

If for example clicking on an imagebutton would go to http://yoursite/LargeImage.aspx?ShowImage=me.png,
add
'&LVSelIndex=[the index]' for each imagebutton
Avatar of Anil Golamari
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim i As Integer
        i = ListBox1.SelectedIndex.ToString()
        Label1.Text = i
   End Sub

this should work.  Add .tostring()
Avatar of bham3dman

ASKER

klakkas, thanks but is there no way to get the index of the listview item?  How would I at a later point, tell the datapager which page to display by using the imagebutton index?

lucky85, I did try this but the result is always -1.

Thanks guys.
The thing is that when you click on an imagebutton of a row, that does not necessarily select the row in the ListView.

So, you always get -1 because is reality, no row is actually selected.
I your ListView Click or Double Click event put this code:
Dim SelIndex As Integer

        For Each sItem As ListViewItem In ListView.SelectedItems
            SelIndex = sItem.Index
            messagebox.Show(Selindex)  
     Next
Small Correction
Here you get the index you want:
In your ListView Click or Double Click event put this code:

Dim SelIndex As Integer

        For Each sItem As ListViewItem In ListView.SelectedItems
            SelIndex = sItem.Index
         
     Next
         messagebox.Show(Selindex)  
jtoutou, I tried your code but get an error using ListView.SelectedItems

Error: 'SelectedItems' is not a member of 'System.Web.UI.WebControls.ListView'

ooooo...this is for a windows app..Very sorry ....i'll try again...
Thanks jtoutou. :)
ASKER CERTIFIED SOLUTION
Avatar of John (Yiannis) Toutountzoglou
John (Yiannis) Toutountzoglou
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That works for me jtoutou.  Thanks!  I wound up using the following:

 Protected Sub ListView1_ItemCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewCommandEventArgs) Handles ListView1.ItemCommand

Dim idx As Integer
Dim dataItem As ListViewDataItem = CType(e.Item, ListViewDataItem)
idx = ListView1.Items.IndexOf(dataItem)

This gives me the index of the selected item on the current page.  Now all I have to do is a little math with the datapager to figure out where I need to return.  Thanks again.
Glad i helped you!!!!