Skip to content

Commit 6346bad

Browse files
author
KB Bot
committed
Added new kb article grid-prevent-rowclick-when-selecting-text
1 parent b764967 commit 6346bad

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
---
2+
title: Prevent RowClick Event When Selecting Text in Grid for Blazor
3+
description: Learn how to prevent the RowClick event from being triggered in Grid for Blazor when selecting text using JavaScript interop.
4+
type: how-to
5+
page_title: Avoid RowClick Event Trigger During Text Selection in Grid for Blazor
6+
meta_title: Avoid RowClick Event Trigger During Text Selection in Grid for Blazor
7+
slug: grid-kb-prevent-rowclick-when-selecting-text
8+
tags: grid, blazor, rowclick, text-selection
9+
res_type: kb
10+
ticketid: 1692651
11+
---
12+
13+
## Environment
14+
<table>
15+
<tbody>
16+
<tr>
17+
<td> Product </td>
18+
<td> Grid for Blazor </td>
19+
</tr>
20+
</tbody>
21+
</table>
22+
23+
## Description
24+
25+
I want to prevent the [OnRowClick](slug:grid-events#onrowclick) event in the [Grid for Blazor](slug:grid-overview) from executing when a user selects text by dragging to highlight it.
26+
27+
This knowledge base article also answers the following questions:
28+
- How to prevent row click event in a Blazor Grid during text selection?
29+
- How to check for text selection before firing Grid events?
30+
- How to use JavaScript interop for handling the `OnRowClick` event in Telerik Blazor Grid?
31+
32+
## Solution
33+
34+
To achieve this, use JavaScript interop to detect text selection. Follow these steps:
35+
36+
1. Define a JavaScript helper function that checks whether any text is currently selected on the page.
37+
2. Inject the IJSRuntime service into your Blazor component to enable JavaScript interop.
38+
3. Call the JavaScript function within your `OnRowClick` event handler to bypass logic when text is selected conditionally.
39+
40+
Example implementation:
41+
42+
````RAZOR
43+
@inject IJSRuntime JS
44+
45+
<h3>Grid with Safe Row Click Handling</h3>
46+
47+
@if (!string.IsNullOrEmpty(ClickedPersonName))
48+
{
49+
<p><strong>Last clicked person:</strong> @ClickedPersonName</p>
50+
}
51+
52+
<TelerikGrid Data="@People"
53+
OnRowClick="@OnRowClickHandler"
54+
Height="300px">
55+
<GridColumns>
56+
<GridColumn Field="Id" Title="ID" />
57+
<GridColumn Field="Name" Title="Name" />
58+
<GridColumn Field="Email" Title="Email" />
59+
</GridColumns>
60+
</TelerikGrid>
61+
62+
@code {
63+
public class Person
64+
{
65+
public int Id { get; set; }
66+
public string Name { get; set; } = string.Empty;
67+
public string Email { get; set; } = string.Empty;
68+
}
69+
70+
private readonly List<Person> People = new()
71+
{
72+
new Person { Id = 1, Name = "Alice Johnson", Email = "[email protected]" },
73+
new Person { Id = 2, Name = "Bob Smith", Email = "[email protected]" },
74+
new Person { Id = 3, Name = "Carol Lee", Email = "[email protected]" }
75+
};
76+
77+
private string ClickedPersonName = "";
78+
79+
private async Task OnRowClickHandler(GridRowClickEventArgs args)
80+
{
81+
var isTextSelected = await JS.InvokeAsync<bool>("isTextSelected");
82+
if (isTextSelected)
83+
{
84+
ClickedPersonName = "Text was selected, row click ignored.";
85+
return;
86+
}
87+
88+
var item = (Person)args.Item;
89+
ClickedPersonName = item.Name;
90+
}
91+
}
92+
93+
@* Inline JavaScript for detecting text selection *@
94+
<script>
95+
window.isTextSelected = function () {
96+
return window.getSelection().toString().length > 0;
97+
};
98+
</script>
99+
````
100+
101+
## See Also
102+
103+
* [Grid Events Documentation](slug:grid-events#onrowclick)

0 commit comments

Comments
 (0)