如何通过单击ASP.NET网格视图中一行中的图像按钮来获取行数据

时间:2016-05-26 09:39:29

标签: c# asp.net gridview

我在ASP.NET Web应用程序中有一个GridView,其中我在每行中添加了图像按钮:

 <asp:TemplateField>
       <ItemTemplate>
      <asp:ImageButton ID="edit" runat="server" CommandArgument='<%# Bind("EmpID") %>' CommandName="edituser" ImageUrl="image/images.jpg" 
       ToolTip="Edit User Details"> </asp:ImageButton>
      </ItemTemplate> 
 </asp:TemplateField>

现在我如何通过单击行中的编辑图像按钮来从gridview获取行数据?

2 个答案:

答案 0 :(得分:2)

您必须使用以下命令更改CommandArgument:

CommandArgument="<%# ((GridViewRow) Container).RowIndex %>  

然后:

 protected void GridView1_RowCommand(object sender, 
  GridViewCommandEventArgs e)
{
  if (e.CommandName == "edituser") /*if you need this
  {
    // Retrieve the row index stored in the 
    // CommandArgument property.
    int index = Convert.ToInt32(e.CommandArgument);

    // Retrieve the row that contains the buttonfrom the Rows collection.
    GridViewRow row = GridView1.Rows[index];

    // Add code here now you have the specific row data
  }

  }

答案 1 :(得分:0)

分别绑定标签控件中的所有列,并且可以使用GridView1_SelectedIndexChanged事件中的findcontrol方法获取值

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)

    {

        Label _lblText = (Label)this.GridView1.SelectedRow.FindControl("UR_Desired_Label_ID");

        this.TextBox3.Text = _lblText.Text ;

    }