This is a migrated thread and some comments may be shown as answers.

Select Row on GridButtonColumn click

5 Answers 463 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Trey
Top achievements
Rank 1
Trey asked on 25 Nov 2013, 04:57 PM
Perhaps this is by design, but it seems that if you set the client setting AllowRowSelect="true", the row is not selected when you click on a GridButtonColumn. For example, I have a GridButtonColumn for Delete, and if I select a row, it is selected, but then if I press the Delete button on a different row, the previous row is still selected. When I use ConfirmText, it could be confusing to the user since they might think they are about to delete the selected row instead of the row they clicked the delete button on.

Is there an easy way to have the row selected that the GridButtonColumn was pressed in?

5 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 26 Nov 2013, 04:07 AM
Hi Trey,

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
0
Oliver
Top achievements
Rank 1
answered on 17 Aug 2015, 03:56 PM

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

1
Viktor Tachev
Telerik team
answered on 18 Aug 2015, 01:40 PM
Hello 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
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Abitar
Top achievements
Rank 1
answered on 26 Aug 2015, 09:50 AM

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.

 

0
Viktor Tachev
Telerik team
answered on 28 Aug 2015, 12:04 PM
Hi Abbas,

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
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
Grid
Asked by
Trey
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Oliver
Top achievements
Rank 1
Viktor Tachev
Telerik team
Abitar
Top achievements
Rank 1
Share this question
or