这里我需要获取我点击哪一行的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
答案 0 :(得分:0)
首先,您需要向gridview
添加OnRowCreated
事件
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);
}
}