从 GridView 中的 ImageButton 单击获取行索引
我有一个 Gridview,其中通过模板字段将 ImageButtons 添加到列中。 我已将一个函数附加到“OnClick”事件。
进入此函数后,如何获取具有已单击按钮的行的索引。 看来我所拥有的只是页面上的鼠标坐标。
I have a Gridview with ImageButtons added to a column via a templatefield. I've attached a function to the "OnClick" event.
Once in this function, how can I get the index of the row that has the button that has been clicked. It appears that all I have is the mouse coordinates on the page.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用它,而不是循环遍历行
Instead of looping through the rows you can use this
将发送者投射到 ImageButton,然后将图像按钮的 NamingContainer 投射到一行:
VB:
C#:
Cast the sender to an ImageButton then cast the image button's NamingContainer to a row:
VB:
C#:
同意 bdukes 的观点,最简单的选择是使用 CommandArgument。 将数据的唯一 ID 号绑定到该属性中,然后处理 _RowCommand 事件。
例如:
Would agree with bdukes that the simplest option is to use the CommandArgument. Bind your data's unique ID number into that property, then handle the _RowCommand event.
For example:
这是非常好的技巧。
我还有一个绝招。
你可以尝试一下...
这也是一个好方法。
This is very good trick.
I have another trick also.
You can try it...
It's a also good method.
我发现的最简单的方法是使用 Command 事件通过 Click 事件,并将项目 ID 作为命令参数发送。
您还可以循环 GridView 中的行,并将行中的 ImageButton 与 Click 事件中的
sender
参数进行比较。The easiest way that I've found is to use the Command event over the Click event, and send the item ID as the command argument.
You could also loop over the rows in the GridView and compare the ImageButton in the row to the
sender
argument in your Click event.