0

在此处输入图像描述

在这里,我需要获取我单击的行上的 DataKeyNames(在侧面工具列图像按钮单击中)

但是当我单击图像按钮(铅笔)时,它显示以下错误

在此处输入图像描述

我的 C# 代码是

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Edit")
    {
        ImageButton btnedit = sender as ImageButton;          
        GridViewRow gvrow = btnedit.NamingContainer as GridViewRow;
        int sid = Convert.ToInt32(GridView1.DataKeys[gvrow.RowIndex].Value);
        Response.Write(sid);
    }
    else if (e.CommandName == "Delete")
    { 

    }
}

我的 Gridview 源代码

<asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand" AutoGenerateColumns="False" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" DataKeyNames="StdId" BackColor="White" BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" CellPadding="3" CellSpacing="1" EnableModelValidation="True" GridLines="None">
            <Columns>
                <asp:TemplateField>
                    <HeaderTemplate>
                        <asp:CheckBox ID="CheckBox1" runat="server" />
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:CheckBox ID="CheckBox2" runat="server"></asp:CheckBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Name">
                    <ItemTemplate><%#Eval("Name") %></ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Email">
                    <ItemTemplate><%#Eval("Email") %></ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Mobile">
                    <ItemTemplate><%#Eval("Mobile") %></ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="City">
                    <ItemTemplate><%#Eval("City") %></ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Tools">
                    <ItemTemplate>
                        <asp:ImageButton ID="imgbtnEdit" runat="server" CommandArgument="ImageButton" CommandName="Edit" ImageUrl="~/pencil.png" ToolTip="Click To Edit" AlternateText="Click To Edit"/>
                        <asp:ImageButton ID="imgbtnDelete" runat="server"  CommandName="Delete" ImageUrl="~/cross.png" ToolTip="Click To Dletee" AlternateText="Click To Dletee"/>                            
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>

            <FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
            <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
            <PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
            <RowStyle BackColor="#DEDFDE" ForeColor="Black" />
            <SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />

        </asp:GridView>

请帮助我如何获取 DatakeyName

4

1 回答 1

0

首先,您需要将一个OnRowCreated事件添加到您的 gridview

OnRowCreated="GridView1_RowCreated"

在 .cs 文件中添加此内容(在 CommandArgument 中设置 Rowindex 值,以便您可以在 RoeCommand 上获取此内容)

 protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ImageButton imgbtnEdit= (ImageButton)e.Row.FindControl("lnkUrlID");
                imgbtnEdit.CommandArgument = e.Row.RowIndex.ToString();

                ImageButton imgbtnDelete= (ImageButton )e.Row.FindControl("lnkExtend");
                imgbtnDelete.CommandArgument = e.Row.RowIndex.ToString();

            }
        }

现在在你的 GridView1_RowCommand 试试这个

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
  if (e.CommandName == "Edit")
  {
    int sid = Convert.ToInt32(GridView1.DataKeys[Convert.ToInt32(e.CommandArgument)].Value);
  }

}
于 2013-11-14T09:47:10.193 回答