.net – ASP.NET ListView full row select

asp.netgridviewlistviewnet

So I am playing around with using a ListView instead of a GridView to accomplish a complicated goal. The ListView is helping in a lot of ways, but there is one particular bit of code I am used to using with GridView's that won't doesn't work with ListView's.

If I have to have a nice mouse hover action on my rows in a GridView, and if I want to let a user click anywhere in a row to select it, I use the OnRowDataBound event and do something like this

e.Row.Attributes["onmouseover"] = "this.oldClass=this.className;this.className='hover';";
e.Row.Attributes["onmouseout"] = "this.className=this.oldClass;";
e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(gdvBuildings, "Select$" + e.Row.RowIndex.ToString());

It works great with GridViews. With the ListView I can use the OnItemDataBound event, but there doesn't seem to be any control that has an Attributes array to add to.

Does anyone know of an equivalent solution to allow for mouse hover and full row click with a ListView?

Best Answer

On a ListView you are creating the row yourself so you can add this functionality directly on the row, something like this.

<asp:ListView ID="ListView3" runat="server">
<ItemTemplate>
  <tr onmouseover="this.oldClass=this.className;this.className='hover';" onmouseout="this.className=this.oldClass;" onclick='<%# Page.ClientScript.GetPostBackClientHyperlink(gdvBuildings, "Select$" + Container.DataItemIndex.ToString()) %>' >
    <td>
      <asp:Label ID="Label7" runat="server" Text='<%# Eval("ClientNumber") %>' />
    </td>
    <td>
      <asp:Label ID="CityNameLabel" runat="server" Text='<%# Eval("FullName") %>' />
    </td>
  </tr>
</ItemTemplate>
<LayoutTemplate>
  <table id="itemPlaceholderContainer" runat="server" border="0" style="">
    <tr id="itemPlaceholder" runat="server">
    </tr>
  </table>
</LayoutTemplate>
</asp:ListView>