Click here to Skip to main content
15,882,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Evening All,

I have come across a bit of a problem that I can't get around, I have checked online and I can't seem to find the answer I am looking for.

I have a listview that is being used a search results box, for example user enters text, clicks search button, returns results of search as a list of items in the list view.

What I am trying to do is select each item when it is clicked on and do some code on a button press. So for example in this case I want a user to click on an item in the listview, click add to profile button and the item will be added to a profile.

I have no idea how you get the selected item in a listview and I am really struggling with it. I am basically trying to create the functionality of a listbox with the added columns of a Listview.

Any help would be much appreciated.

Dan
Posted

1 solution

I'm not sure if I've got your problem in the right way, I think you just need to set an ItemCommand event handler for your listview and then set the CommandName and CommandArgument in your button (or in any other control).

ASP.NET
<asp:listview runat="server" xmlns:asp="#unknown">
        ID="TheAnimalsListView"
        OnItemCommand="TheAnimalsListView_OnItemCommand"
        DataKeyNames="TheID">
        <layouttemplate>
          [...]
        </layouttemplate>
        <itemtemplate>
              <asp:linkbutton runat="server">
                ID="SelectAnimalButton" 
                Text="Show This" 
                CommandName="ShowThis" 
                CommandArgument='<%#Eval("AnimalName")%>' />
        </asp:linkbutton></itemtemplate>
      </asp:listview>


Then, in the codebehind

C#
protected void TheAnimalsListView_OnItemCommand(object sender, ListViewCommandEventArgs e)
  {
    if (String.Equals(e.CommandName, "ShowThis"))
      {
        //Shows the image of the animal based on the AnimalName
        ShowAnimalImage(e.CommandArgument.ToString());
      }
    }
  }
 
Share this answer
 
v2
Comments
DanHodgson88 29-Nov-12 20:13pm    
Thank you for your answer Daniele, this wasn't quite what I was looking for, possibly my poorly written question!

In a ListBox a row is selected when a user clicks it, from here you can obviously do whatever you like with the item using something like listbox1.selecteditem.ToString(). I am looking to implement the identical functionality in a listview so basically listview.selecteditem.ToString().

Thanks again.
Dan

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900