4

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?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
PercivalMcGullicuddy
  • 5,263
  • 9
  • 46
  • 65
  • Hey, I'm sorry but Im not familiarized with telerik controls. However my company came very close to use telerik controls, and one of the telerik great atratives is the support. They have one of the best forums and support for custom controls. Maybe you should try it. You can ask questions even if you dont have a telerik license. –  Oct 14 '11 at 18:34

2 Answers2

7

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.

KreepN
  • 8,528
  • 1
  • 40
  • 58
2

Please check Below code snippet.

   <MasterTableView  DataKeyNames="ID">

<Columns>
                    <telerik:GridBoundColumn DataField="Name" HeaderText="Name" UniqueName="Name">
                    </telerik:GridBoundColumn>
                    <telerik:GridTemplateColumn HeaderText="Actions">
                        <ItemTemplate>
                            <asp:Button ID="btnEdit" runat="server" ToolTip="Edit" CommandName="Edit" />&nbsp;&nbsp;&nbsp;<asp:Button
                                ID="btnDelete" runat="server" ToolTip="Delete" CommandName="Delete" />
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                </Columns>

.........................

protected void grdCompCliente_ItemCommand(object sender, GridCommandEventArgs e)
{
        if (e.CommandName == "Edit")
        {
            GridDataItem item = e.Item as GridDataItem;
            string ID = item.GetDataKeyValue("ID").ToString();
            string Name = item["Name"].Text;
        }
        else if (e.CommandName == "Delete")
        {
            GridDataItem item = e.Item as GridDataItem;
            string ID = item.GetDataKeyValue("ID").ToString();
            string Name = item["Name"].Text;
        }
}
Jayesh Goyani
  • 11,008
  • 11
  • 30
  • 50