Kusum
Kusum

Reputation: 497

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: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?

Upvotes: 1

Views: 1865

Answers (2)

Giannis Grivas
Giannis Grivas

Reputation: 3412

You have to change the CommandArgument with this one:

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
  }

  }

Upvotes: 2

hamzeh.hanandeh
hamzeh.hanandeh

Reputation: 743

Bind all the columns in the label control respectively, and you can get value using findcontrol method at GridView1_SelectedIndexChanged event

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)

    {

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

        this.TextBox3.Text = _lblText.Text ;

    }

Upvotes: 0

Related Questions