tags:

views:

1123

answers:

5

I'm attempting to do something which should be relatively simple. I'm using a ListView to show a number of items, using the ItemTemplate. Inside the ItemTemplate I have a div surrounding the full item, so that I can highlight on a hover event, (being handled already by jQuery).

What I would like to do is have the click event on the div fire a button to open a details view for that specific item. The problem I'm having is pulling back the ClientID for that button while using jQuery. I've tried the:

$('#<%= Button1.ClientID %>')

route, but that doesn't work, as the Button doesn't exist when the page loads.

The basic code is as follows:

<asp:listview>
 <itemtemplate>
  <td runat="server">
   <div class="container">
    <asp:linkbutton id="Button1" runat="server" text="View Details" />
     fun and exciting stuff here...
   </div>
  </td>
 </itemtemplate>
</asp:listview>

Any suggestions or help are much appreciated!

+2  A: 

Wire up a method to the ItemDataBound event of your ListView. In there, you can use FindControl method on the Item property of the ListViewItemEventArgs parameter to get a reference to that LinkButton for each item in your list. From that reference, you can get the ClientID, and do with it whatever you need to do.

UPDATE
In terms of what you can do with the reference to the button, the easiest is probably to just assign your click event in the ItemDataBound event, maybe something like this

var button = (Button)e.Item.FindControl("Button1");
button.OnClientClick = "ShowEditDialog(" + Eval("ItemId") + ")";

If you need to wire it up on the client side, maybe you could maintain a dictionary mapping ClientIDs to Item IDs, and send that dictionary down to the client, where you could iterate over it and wire the events up.

bdukes
While your solution would seem to work at first, the problem is injecting the ClientID back into the page so that I could reference it with jQuery on the client-side. Maybe I'm not understanding what you mean?
riceboyler
Exactly what I needed! Thanks for the fix!
riceboyler
+4  A: 

Why don't you just give those linkbuttons a specific css class?

<asp:linkbutton id="Button1" runat="server" text="View Details" CssClass="detailsButtons" />

Then in jquery just select the class to assign them all in one call.

$(".detailsButtons").click( function() { .... } );

If that doesn't work because the objects aren't made at that time, you can use the new live() method in jquery. It will assign the events to any dynamically added elements as well.

Hope this helps

Jab
Very interesting idea, with the live() method. I might give that a shot. The problem with the class idea is that I need to fire the click() event on a specific button, so assigning all of the buttons at once wouldn't accomplish what I need. However, the live thing may work, so I'm gonna try it!
riceboyler
+1 Where the items belong to a logical class they should be referenced as such. This is exactly the right solution (where bdukes gives the actual answer).
annakata
+1  A: 

If it's the click event that you are after can't you use the OnClientClick property of the linkbutton? or did they remove that in 3.5?

Al W
+1  A: 

You can select your buttons with this syntax (assuming Button1 is the server id of the button):

$("[id$=Button1]")

that will select all the DOM element whose id ends with "Button1".

It is necessary because the resultant client id of an element reflects the server-control structure.

tanathos
+2  A: 

Instead of using the scriptlet:

$('#<%= Button1.ClientID %>')

use a binding expression:

$('#<%# Button1.ClientID %>')

Notice the difference is the # instead of the =.

Binding expressions are evaluated when the data is bound, and therefore your expression will evaluate in the context of the current data item. Scriptlets are evaluated when the page is rendered (at the very end of the page lifecycle), and evaluate in the context of the page, not a particular data item.

Trent