当我点击gridview中的任何行时,为什么选择第一行的索引?

时间:2015-10-30 07:05:04

标签: c# asp.net gridview webforms

当我在任何一行中单击EDIT时,为什么选择第一行的索引?

Gridiview中的编辑按钮:

<asp:ButtonField CommandName="cmdEdit" HeaderText="Edit" ImageUrl="~/assets/global/images/shopping/edit.png" ButtonType="Image" ControlStyle-Width="25px" ControlStyle-Height="20px" />

编辑背后的代码:

protected void grdviewLandInfo_RowCommand(object sender, GridViewCommandEventArgs e)
{
    try
    {              
         int Index = Convert.ToInt32(e.CommandArgument);
         Int32 LandInfoID =  Convert.ToInt32(grdviewLandInfo.DataKeys[Index].Value);
         short UserID = Convert.ToInt16(Session["UserID"]);

         short LandTypeID = Convert.ToInt16(grdviewLandInfo.Rows[Index].Cells[6].Text);

3 个答案:

答案 0 :(得分:0)

在commandArgument属性中添加buttonfield,如下所示。

<asp:ButtonField CommandName="cmdEdit" HeaderText="Edit" ImageUrl="~/assets/global/images/shopping/edit.png" CommandArgument='<%# ((GridViewRow) Container).RowIndex %>' ButtonType="Image" ControlStyle-Width="25px" ControlStyle-Height="20px" />

答案 1 :(得分:0)

试试这个:

protected void grdviewLandInfo_RowCommand(object sender, GridViewCommandEventArgs e)
{
  if(e.CommandName == "cmdEdit")
   {
    try
    {  
       ...

答案 2 :(得分:0)

试试这个...

if (e.CommandName.Equals("cmdEdit"))
{
        GridViewRow row = (GridViewRow)((Control)e.CommandSource).Parent.Parent;
    Int32 LandInfoID =  Convert.ToInt32(grdviewLandInfo.DataKeys[row.RowIndex].Value);
}

希望这会有所帮助......