Is there an easy way to have the row selected that the GridButtonColumn was pressed in?
5 Answers, 1 is accepted
Please try the following code snippet to select a row on GridButtonColumn click.
ASPX:
<
telerik:GridButtonColumn
ButtonType
=
"ImageButton"
CommandName
=
"Delete"
Text
=
"Delete"
UniqueName
=
"DeleteColumn"
/>
C#:
protected
void
RadGrid1_ItemDataBound(
object
sender, GridItemEventArgs e)
{
if
(e.Item
is
GridDataItem)
{
GridDataItem item = (GridDataItem)e.Item;
ImageButton imgbtn = (ImageButton)item[
"DeleteColumn"
].Controls[0];
imgbtn.Attributes.Add(
"OnClick"
,
"return Click('"
+ item.ItemIndex +
"');"
);
}
}
JS:
<script type=
"text/javascript"
language=
"javascript"
>
function
Click(RowIndex)
{
var
RadGrid1 = $find(
"<%= RadGrid1.ClientID %>"
);
RadGrid1.get_masterTableView().get_dataItems()[RowIndex].set_selected(
"true"
);
var
row = RadGrid1.get_masterTableView().get_dataItems()[RowIndex].get_element();
var
Name = row.cells[1].innerHTML;
var
agree = confirm(
'Do you want to delete the Product: '
+ Name +
'?'
);
if
(agree)
{
return
true
;
}
else
{
RadGrid1.get_masterTableView().get_dataItems()[RowIndex].set_selected(
"false"
);
return
false
;
}
}
</script>
Thanks,
Princy
Hi Princy!
I tried also this solution. Works fine for the 1st click. The 2nd click in another row doesn't work. I found out that if I click in the same row of the 1st click the row is deselected and then I can click in another row which is then selected.
Do you have an idea what is wrong?
Thank you,
Oliver
I have assembled a sample project using the suggested approach. The items are selected as expected on my end. I am attaching the project as reference. Please give it a try and see how it works for you.
Regards,
Viktor Tachev
Telerik
Hello,
Can't I get the row index on GridButtonColumn click without firing an ItemDataBound or ItemCreated handlers ?
I need to do this on client side using javascript only.
I tried to do this using ItemDataBound event, It worked well for update commands but when I insert a new item to the RadGrid, and then click on the GridButtonColumn, I loose unsaved data in the RadGrid (Batch).
What can do to resolve this issue ?
Thanks for the help !
Abbas B.
If you would like to add confirm message when Batch Editing you can use a GridClientDeleteColumn. It enables you to prompt the user before deleting a record.
<
telerik:GridClientDeleteColumn
ConfirmText
=
"Are you sure you would like to delete the record?"
></
telerik:GridClientDeleteColumn
>
Regards,
Viktor Tachev
Telerik