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

Prevent users rowselect RadGrid

5 Answers 200 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Gilberto
Top achievements
Rank 1
Veteran
Gilberto asked on 05 May 2020, 08:18 PM

Hello, guys.

I have a grid and I need to select/deselect rows progammatically in javascript depending on some operations (This is already done).

Now, I need to prevent user on selecting/deselecting rows by clicking. Im creating the grid completelly in my code behind and this is how I'm trying to achieve this behavior with no success:

 

VB

With GridListaItm.ClientSettings
  .AllowKeyboardNavigation = False
  .AllowColumnsReorder = False
  .EnablePostBackOnRowClick = False
  .EnableRowHoverStyle = True
 
  .Selecting.AllowRowSelect = True
  .Selecting.EnableDragToSelectRows = False
 
  .Resizing.AllowColumnResize = True
  .Resizing.AllowResizeToFit = True
 
  .Scrolling.AllowScroll = True
  .Scrolling.UseStaticHeaders = True
  .Scrolling.SaveScrollPosition = True
  .Scrolling.FrozenColumnsCount = 3
 
  .ClientEvents.OnRowSelecting = "RowSelecting"
  .ClientEvents.OnRowSelecting = "RowSelecting"
  .ClientEvents.OnRowClick = "RClic"
 End With

JS

<script type="text/javascript">    
       var snClic=0;
       var snCambio=0;
 
       function RowSelecting(sender, eventArgs) {
 
           if (snCambio == 1) { snCambio = 0; return 0;}
           if (snClic == 1) { eventArgs.set_cancel(true); snCambio = 1; snClic = 0;}
 
       }
 
       function RClic(sender, eventArgs) {
           snClic = 1;
           snCambio = 0;
       }
 
   </script>

5 Answers, 1 is accepted

Sort by
0
Doncho
Telerik team
answered on 08 May 2020, 12:57 PM

Hello Gilberto,

Normally, canceling the RowSelecting event is the correct approach to prevent client selection, see OnRowSelecting article. Using only this event would be enough. 

You may also check out the instructions for accessing Rows/Items in the Accessing Values and Controls article if you would like to access cells or their values on the client-side. Here is a nice article showing how to select a cell and access its content both on the client and server sides: Select cells in RadGrid with Hierarchy

I see, however, that the code you shared attaches both RowClick and RowSelecting events. Is there a particular reason for that? If the Grid is configured to make a postback when clicking on a row, the RowSelection will not be cancelled as the RowClick event will interrupt the logic and do the Postback anyway.

Looking forward to hearing from you!

Kind regards,
Doncho
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
0
Gilberto
Top achievements
Rank 1
Veteran
answered on 08 May 2020, 01:23 PM

Hello.

Im doing everything at client side so postback on rowclick is not enabled.

I use rowclic client-event because the selection was been cancelled even when I was selecting progamatically so, in rowclick I set a variable that let me know the user tried to select/unselect by clicking and just un that case, I cancel the event.

Im gonna check the resourses you provided and I'll let you know.

0
Gilberto
Top achievements
Rank 1
Veteran
answered on 08 May 2020, 01:47 PM
I already read the articles and I see that my logic is ok but the cancel instruction "eventArgs.set_cancel(true)" is not working all the times. Is there a reason for this?
0
Accepted
Doncho
Telerik team
answered on 13 May 2020, 01:22 PM

Hi Gilberto,

If you want to prevent the users from clicking on a row, you will need to implement a JavaScript/jQuery method that listens to the click event, especially when the grid row is clicked, and in that event handler, cancel the event.

 

For example:

$(document).ready(function () {
    $('.RadGrid .rgMasterTable > tbody > tr').on('click', function (e) {
        e.preventDefault();
    })
})

 

This will not prevent Row Selection made programmatically, as that is not coming from "clicking on a row".

I hope this will prove helpful.

Kind regards,
Doncho
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
0
Gilberto
Top achievements
Rank 1
Veteran
answered on 13 May 2020, 04:22 PM

Hello. Thank you very very much for your answer. I tried your example and it is working perfect. 

This is just what I needed to make it work:

if (args.get_domEvent().type == "click")

I did not know how to get the clic event. Im new in this.

 

 

Tags
Grid
Asked by
Gilberto
Top achievements
Rank 1
Veteran
Answers by
Doncho
Telerik team
Gilberto
Top achievements
Rank 1
Veteran
Share this question
or