Solved

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

Posted on 2010-09-15
14
605 Views
Last Modified: 2013-11-26
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. :)
0
Comment
Question by:bham3dman
  • 5
  • 4
  • 3
  • +1
14 Comments
 
LVL 7

Expert Comment

by:klakkas
ID: 33683272
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.



0
 
LVL 7

Expert Comment

by:klakkas
ID: 33683309
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
0
 
LVL 18

Expert Comment

by:Anil Golamari
ID: 33683377
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()
0
 
LVL 5

Author Comment

by:bham3dman
ID: 33683458
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.
0
 
LVL 7

Expert Comment

by:klakkas
ID: 33683538
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.
0
 
LVL 18

Expert Comment

by:Anil Golamari
ID: 33683612
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33683754
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
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33684212
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)  
0
 
LVL 5

Author Comment

by:bham3dman
ID: 33684640
jtoutou, I tried your code but get an error using ListView.SelectedItems

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

0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33684691
ooooo...this is for a windows app..Very sorry ....i'll try again...
0
 
LVL 5

Author Comment

by:bham3dman
ID: 33684697
Thanks jtoutou. :)
0
 
LVL 18

Accepted Solution

by:
John (Yiannis) Toutountzoglou earned 500 total points
ID: 33684735
0
 
LVL 5

Author Comment

by:bham3dman
ID: 33685062
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.
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33685323
Glad i helped you!!!!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now