【问题标题】:How to track a specific Button from gridview如何从gridview跟踪特定按钮
【发布时间】:2017-02-25 16:00:35
【问题描述】:

我在每一行都有一个带有模板按钮的gridview 每个按钮都会触发一个Onclick 事件。

我需要跟踪按下按钮的行。 如果我想,让我们说给背景上色,我该怎么做?

<asp:GridView ID="OrdersGridView" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None">
    <AlternatingRowStyle BackColor="White" />
    <Columns>
        <asp:TemplateField HeaderText="Delete">
            <ItemTemplate>
                <asp:Button ID="DeleteOrder" runat="server" BackColor="Red" Font-Bold="True" ForeColor="White" Text="מחק" OnClick="DeleteOrder_Click" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="OrderHost" HeaderText="OrderName" />
        <asp:BoundField DataField="OrderId" Visible="False" />
    </Columns>
    <EditRowStyle BackColor="#2461BF" />
    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
    <RowStyle BackColor="#EFF3FB" />
    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
    <SortedAscendingCellStyle BackColor="#F5F7FB" />
    <SortedAscendingHeaderStyle BackColor="#6D95E1" />
    <SortedDescendingCellStyle BackColor="#E9EBEF" />
    <SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>

事件为空。

protected void DeleteOrder_Click(object sender, EventArgs e)
    {

    }

【问题讨论】:

  • 您可能需要指定您使用的语言。
  • C#,已编辑标签。
  • 你在使用 UWP 吗?
  • 我的意思是你在开发 UWP、WPF 或 Web 应用程序?

标签: c# asp.net events gridview


【解决方案1】:

根据您的评论,您只想获取GridView 的索引。

protected void DeleteOrder_Click(object sender, System.EventArgs e)
{
  Button btn = (Button)sender;
  GridViewRow row = (GridViewRow)btn.NamingContainer;
  //Get the Row Index by calling row.RowIndex;
}  

希望对你有帮助!

【讨论】:

  • 完美!只要你能解释一下“NamingContainer”是什么,那就太好了!
  • NamingContainer 返回的是继承INamingContainer 接口的最顶层容器。和Parent有点类似,和Parent不同的是它只获取控制树中的直接父级。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多