Telerik - Image button inside a Grid row, getting value of first cell
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" /> <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?
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.
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" /> <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;
}
}
精彩评论