Telerik - Image button inside a Grid row, getting value of first cell

PercivalMcGullicuddy picture PercivalMcGullicuddy · Oct 14, 2011 · Viewed 10.4k times · Source

I have a RadGrid that contains a template column in which I've put two image buttons for edit and delete actions.

<telerik:GridTemplateColumn HeaderText="Actions">    
   <ItemTemplate>
     <asp:ImageButton ID="btnEdit" runat="server"  ImageUrl="~/images/icon_edit.png" style="display: inline-block" ToolTip="Edit"  />&nbsp;&nbsp;&nbsp;<asp:ImageButton ID="btnDelete" runat="server" ImageUrl="~/images/icon_delete.png" style="display: inline-block" ToolTip="Delete" />
      </ItemTemplate>
</telerik:GridTemplateColumn>

How would get the value of the first cell of the row (datafield = "User_ID") when I click on the "delete" button?

Answer

KreepN picture KreepN · Oct 14, 2011

Step 1.

Go to the Radgrid itself and edit the field DataKeyNames="" (under MasterTableView) and add the datafield you are pulling:

<MasterTableView ... DataKeyNames="User_ID">

Step 2. Edit the CommandName="" property of the Imagebuttons located in the grid:

 <asp:ImageButton ID="btnDelete" runat="server" style="display: inline-block" ToolTip="Delete" CommandName="dosomething"/>

Create the following method for your Radgrid and add this code:

protected void RadGrid1_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
{
   if (e.CommandName == "dosomething")
    {
        //Use a line of code here to save that User_ID that you want from the first column
        theUserId = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["User_ID"];
    }
}

Make sure theUserId = the same Type (int,double,dec...) as the field it's pulling from or you will have to parse it:

theUserId = Int.Parse(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["User_ID"]);

Let me know if you need more help.