|
| 1 | +--- |
| 2 | +title: How to Access GridSearchBox Value |
| 3 | +description: Learn how to retrieve the GridSearchBox value in a GridToolBarTemplate in Telerik UI for Blazor. |
| 4 | +type: how-to |
| 5 | +page_title: Capturing GridSearchBox Input |
| 6 | +meta_title: Capture GridSearchBox Input |
| 7 | +slug: grid-kb-access-gridsearchbox-value |
| 8 | +tags: toolbar, searchbox, datasource, filters, grid |
| 9 | +res_type: kb |
| 10 | +ticketid: 1693363 |
| 11 | +--- |
| 12 | + |
| 13 | +## Environment |
| 14 | + |
| 15 | +<table> |
| 16 | + <tbody> |
| 17 | + <tr> |
| 18 | + <td>Product</td> |
| 19 | + <td>Grid for Blazor</td> |
| 20 | + </tr> |
| 21 | + </tbody> |
| 22 | +</table> |
| 23 | + |
| 24 | +## Description |
| 25 | + |
| 26 | +I need to capture the text entered in the [`GridSearchBox`](slug:grid-searchbox) within the `GridToolBarTemplate`. |
| 27 | + |
| 28 | +## Solution |
| 29 | + |
| 30 | +The `GridSearchBox` does not expose a direct `Value` property or provide two-way binding. It is integrated with the Grid's filtering system, automatically converting the search text into filters passed to the Grid. To capture the search text, extract it from the Grid [state](slug:grid-state). Follow these steps: |
| 31 | + |
| 32 | +1. Use the [`OnStateChanged` event](slug:grid-state#onstatechanged) of the Grid to access the current filters. |
| 33 | +2. Extract the search text from the component state. |
| 34 | + |
| 35 | +Here is an example: |
| 36 | + |
| 37 | +````Razor |
| 38 | +@using Telerik.DataSource |
| 39 | +
|
| 40 | +<TelerikButton OnClick="@GetSearchValue">Get Search Value</TelerikButton> |
| 41 | +
|
| 42 | +<TelerikGrid @ref="GridRef" |
| 43 | + Data="@Employees" |
| 44 | + FilterMode="@GridFilterMode.FilterRow" |
| 45 | + Sortable="true" |
| 46 | + Pageable="true" |
| 47 | + OnStateChanged="@((GridStateEventArgs<Employee> args) => GridStateChanged(args))"> |
| 48 | + <GridToolBar> |
| 49 | + <GridSearchBox Placeholder="Search by Name" /> |
| 50 | + </GridToolBar> |
| 51 | + <GridColumns> |
| 52 | + <GridColumn Field=@nameof(Employee.ID) /> |
| 53 | + <GridColumn Field=@nameof(Employee.Name) Title="Name" /> |
| 54 | + <GridColumn Field=@nameof(Employee.HireDate) Title="Hire Date" /> |
| 55 | + </GridColumns> |
| 56 | +</TelerikGrid> |
| 57 | +
|
| 58 | +<p>Current Search: @CurrentSearchValue</p> |
| 59 | +
|
| 60 | +@code { |
| 61 | + private TelerikGrid<Employee> GridRef { get; set; } |
| 62 | + private string CurrentSearchValue { get; set; } = string.Empty; |
| 63 | +
|
| 64 | + private List<Employee> Employees { get; set; } = new(); |
| 65 | +
|
| 66 | + protected override void OnInitialized() |
| 67 | + { |
| 68 | + Employees = GenerateData(); |
| 69 | + } |
| 70 | +
|
| 71 | + private void GridStateChanged(GridStateEventArgs<Employee> args) |
| 72 | + { |
| 73 | + // Capture search value whenever the grid state changes |
| 74 | + CurrentSearchValue = ExtractSearchValue(args.GridState); |
| 75 | + } |
| 76 | +
|
| 77 | + private void GetSearchValue() |
| 78 | + { |
| 79 | + // Or retrieve the current search value on demand |
| 80 | + var gridState = GridRef.GetState(); |
| 81 | + CurrentSearchValue = ExtractSearchValue(gridState); |
| 82 | + } |
| 83 | +
|
| 84 | + private string ExtractSearchValue(GridState<Employee> gridState) |
| 85 | + { |
| 86 | + return (gridState.SearchFilter as CompositeFilterDescriptor)? |
| 87 | + .FilterDescriptors.OfType<FilterDescriptor>() |
| 88 | + .FirstOrDefault()?.Value?.ToString() ?? string.Empty; |
| 89 | + } |
| 90 | +
|
| 91 | + private List<Employee> GenerateData() |
| 92 | + { |
| 93 | + var result = new List<Employee>(); |
| 94 | + var rand = new Random(); |
| 95 | +
|
| 96 | + for (int i = 0; i < 100; i++) |
| 97 | + { |
| 98 | + result.Add(new Employee() |
| 99 | + { |
| 100 | + ID = i, |
| 101 | + Name = "Name " + i, |
| 102 | + HireDate = DateTime.Now.Date.AddDays(rand.Next(-20, 20)) |
| 103 | + }); |
| 104 | + } |
| 105 | +
|
| 106 | + return result; |
| 107 | + } |
| 108 | +
|
| 109 | + public class Employee |
| 110 | + { |
| 111 | + public int ID { get; set; } |
| 112 | + public string Name { get; set; } |
| 113 | + public DateTime HireDate { get; set; } |
| 114 | + } |
| 115 | +} |
| 116 | +```` |
| 117 | + |
| 118 | +## See Also |
| 119 | + |
| 120 | +* [Grid SearchBox](slug:grid-searchbox) |
| 121 | +* [Grid State](slug:grid-state) |
0 commit comments