简体   繁体   English

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

[英]How to get row data by clicking a image button in a row in an ASP.NET gridview

I have a GridView in a ASP.NET web application, in which I have added image button in each row: 我在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>

Now how I can get the row data from gridview simply by clicking an edit image button in a row? 现在,如何仅通过单击一行中的“编辑图像”按钮就可以从gridview获取行数据?

You have to change the CommandArgument with this one: 您必须使用以下命令更改CommandArgument:

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

Then: 然后:

 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
  }

  }

Bind all the columns in the label control respectively, and you can get value using findcontrol method at GridView1_SelectedIndexChanged event 分别绑定标签控件中的所有列,然后可以在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 ;

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM