从 GridView 中的 ImageButton 单击获取行索引

发布于 2024-07-11 13:29:15 字数 131 浏览 6 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

來不及說愛妳 2024-07-18 13:29:16

您可以使用它,而不是循环遍历行

<asp:ImageButton runat="server" id="ibtn1" ... RowIndex='<%# Container.DisplayIndex %>' 
OnClick="button_click"/>

...

protected void button_click(object sender, EventArgs e){
    ImageButton ibtn1 = sender as ImageButton;
    int rowIndex = Convert.ToInt32(ibtn1.Attributes["RowIndex"]);

    //Use this rowIndex in your code
}

Instead of looping through the rows you can use this

<asp:ImageButton runat="server" id="ibtn1" ... RowIndex='<%# Container.DisplayIndex %>' 
OnClick="button_click"/>

...

protected void button_click(object sender, EventArgs e){
    ImageButton ibtn1 = sender as ImageButton;
    int rowIndex = Convert.ToInt32(ibtn1.Attributes["RowIndex"]);

    //Use this rowIndex in your code
}
苏佲洛 2024-07-18 13:29:16

将发送者投射到 ImageButton,然后将图像按钮的 NamingContainer 投射到一行:

VB:

Dim btn as ImageButton = CType(sender, ImageButton)

Dim row as GridViewRow = CType(btn.NamingContainer, GridViewRow)

C#:

ImageButton btn = (ImageButton)sender;

GridViewRow row = (GridViewRow)btn.NamingContainer;

Cast the sender to an ImageButton then cast the image button's NamingContainer to a row:

VB:

Dim btn as ImageButton = CType(sender, ImageButton)

Dim row as GridViewRow = CType(btn.NamingContainer, GridViewRow)

C#:

ImageButton btn = (ImageButton)sender;

GridViewRow row = (GridViewRow)btn.NamingContainer;
哭泣的笑容 2024-07-18 13:29:16

同意 bdukes 的观点,最简单的选择是使用 CommandArgument。 将数据的唯一 ID 号绑定到该属性中,然后处理 _RowCommand 事件。

例如:

<asp:TemplateField >
    <HeaderStyle Width="20" />
    <ItemTemplate>
        <asp:ImageButton ImageUrl="images/icons/iCal.png" CommandArgument='<%# Eval("Event_ID") %>' ToolTip="iCal" runat="server" Height="18" Width="18" />
    </ItemTemplate>
</asp:TemplateField>


Protected Sub gv_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gv.RowCommand

   e.CommandArgument    'use this value in whatever way you like

End Sub

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:

<asp:TemplateField >
    <HeaderStyle Width="20" />
    <ItemTemplate>
        <asp:ImageButton ImageUrl="images/icons/iCal.png" CommandArgument='<%# Eval("Event_ID") %>' ToolTip="iCal" runat="server" Height="18" Width="18" />
    </ItemTemplate>
</asp:TemplateField>


Protected Sub gv_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gv.RowCommand

   e.CommandArgument    'use this value in whatever way you like

End Sub
番薯 2024-07-18 13:29:16

这是非常好的技巧。
我还有一个绝招。
你可以尝试一下...

 protected void userGridview_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Select")
        {
            GridViewRow rowSelect = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
            int rowindex = rowSelect.RowIndex;
         }
    }

这也是一个好方法。

This is very good trick.
I have another trick also.
You can try it...

 protected void userGridview_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Select")
        {
            GridViewRow rowSelect = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
            int rowindex = rowSelect.RowIndex;
         }
    }

It's a also good method.

一页 2024-07-18 13:29:16

我发现的最简单的方法是使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文