如何在asp:listview中拉出asp:button的clientID以与jQuery一起使用? [英] How do I pull the clientID of an asp:button inside an asp:listview for use with jQuery?

查看:69
本文介绍了如何在asp:listview中拉出asp:button的clientID以与jQuery一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试做一些应该比较简单的事情.我正在使用ListView使用ItemTemplate显示许多项目.在ItemTemplate的内部,我有一个围绕整个项目的div,以便可以突出显示悬停事件(已由jQuery处理).

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).

我想做的是让div上的click事件触发一个按钮以打开该特定项目的详细信息视图.我遇到的问题是在使用jQuery时回退该按钮的ClientID.我已经尝试过了:

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 %>')

路由,但这不起作用,因为页面加载时Button不存在.

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

基本代码如下:

<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!

推荐答案

将方法连接到 FindControl 方法 Item <属性rel ="nofollow noreferrer"> ListViewItemEventArgs 参数以获取列表中每个项目对该LinkBut​​ton的引用.从该参考中,您可以获取ClientID,并可以执行任何操作.

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.

更新
根据您对按钮的引用可以可以做什么,最简单的方法可能就是在ItemDataBound事件中分配您的click事件,也许像这样

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") + ")";

如果您需要在客户端连接它,也许您可​​以维护一个将ClientID映射到Item ID的字典,然后将该字典发送给客户端,在那里您可以对其进行迭代并关联事件.

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.

这篇关于如何在asp:listview中拉出asp:button的clientID以与jQuery一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆