|
| 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: Capturing 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 `DataSourceRequest.Filters` during the [`OnRead` event](slug:components/grid/manual-operations) processing. Follow these steps: |
| 31 | + |
| 32 | +1. Use the `OnRead` event of the Grid to access the current filters. |
| 33 | +2. Extract the search text from `DataSourceRequest.Filters`. |
| 34 | + |
| 35 | +Here is an example: |
| 36 | + |
| 37 | +````Razor |
| 38 | +@using Telerik.DataSource |
| 39 | +@using Telerik.DataSource.Extensions |
| 40 | +
|
| 41 | +@( new MarkupString(output) ) |
| 42 | +
|
| 43 | +<br /> |
| 44 | +<TelerikButton OnClick="@GetFilters">Get Filters</TelerikButton> |
| 45 | +
|
| 46 | +<TelerikGrid TItem="@Employee" OnRead="@ReadItems" |
| 47 | + FilterMode="@GridFilterMode.FilterRow" |
| 48 | + Sortable="true" Pageable="true"> |
| 49 | + <GridToolBar> |
| 50 | + <GridSearchBox Placeholder="Search by Name" /> |
| 51 | + </GridToolBar> |
| 52 | + <GridColumns> |
| 53 | + <GridColumn Field=@nameof(Employee.ID) /> |
| 54 | + <GridColumn Field=@nameof(Employee.Name) Title="Name" /> |
| 55 | + <GridColumn Field=@nameof(Employee.HireDate) Title="Hire Date" /> |
| 56 | + </GridColumns> |
| 57 | +</TelerikGrid> |
| 58 | +
|
| 59 | +@code { |
| 60 | + private List<Employee> SourceData { get; set; } |
| 61 | + private string output { get; set; } |
| 62 | + private DataSourceRequest CurrentRequest { get; set; } |
| 63 | +
|
| 64 | + private void GetFilters() |
| 65 | + { |
| 66 | + output = string.Empty; |
| 67 | +
|
| 68 | + foreach (var item in CurrentRequest.Filters) |
| 69 | + { |
| 70 | + if (item is FilterDescriptor) // filter row |
| 71 | + { |
| 72 | + FilterDescriptor currFilter = item as FilterDescriptor; |
| 73 | + output += $"field: {currFilter.Member}, operator {currFilter.Operator}, value: {currFilter.Value}<br />"; |
| 74 | + } |
| 75 | +
|
| 76 | + if (item is CompositeFilterDescriptor) // filter menu |
| 77 | + { |
| 78 | + CompositeFilterDescriptor currFilter = item as CompositeFilterDescriptor; |
| 79 | + output += $"START nested filter: logical operator: {currFilter.LogicalOperator}, details:<br />"; |
| 80 | + |
| 81 | + foreach (FilterDescriptor nestedFilter in currFilter.FilterDescriptors) |
| 82 | + { |
| 83 | +
|
| 84 | + output += $"field: {nestedFilter.Member}, operator {nestedFilter.Operator}, value: {nestedFilter.Value}<br />"; |
| 85 | + } |
| 86 | + output += "END nested filter<br />"; |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | +
|
| 91 | + protected async Task ReadItems(GridReadEventArgs args) |
| 92 | + { |
| 93 | + CurrentRequest = args.Request; //store the current request for later use |
| 94 | +
|
| 95 | + await Task.Delay(1000); //simulate network delay from a real async call |
| 96 | +
|
| 97 | + var datasourceResult = SourceData.ToDataSourceResult(args.Request); |
| 98 | +
|
| 99 | + args.Data = datasourceResult.Data; |
| 100 | + args.Total = datasourceResult.Total; |
| 101 | + } |
| 102 | +
|
| 103 | + protected override void OnInitialized() |
| 104 | + { |
| 105 | + SourceData = GenerateData(); |
| 106 | + } |
| 107 | +
|
| 108 | + private List<Employee> GenerateData() |
| 109 | + { |
| 110 | + var result = new List<Employee>(); |
| 111 | + var rand = new Random(); |
| 112 | + for (int i = 0; i < 100; i++) |
| 113 | + { |
| 114 | + result.Add(new Employee() |
| 115 | + { |
| 116 | + ID = i, |
| 117 | + Name = "Name " + i, |
| 118 | + HireDate = DateTime.Now.Date.AddDays(rand.Next(-20, 20)) |
| 119 | + }); |
| 120 | + } |
| 121 | +
|
| 122 | + return result; |
| 123 | + } |
| 124 | +
|
| 125 | + public class Employee |
| 126 | + { |
| 127 | + public int ID { get; set; } |
| 128 | + public string Name { get; set; } |
| 129 | + public DateTime HireDate { get; set; } |
| 130 | + } |
| 131 | +} |
| 132 | +```` |
| 133 | + |
| 134 | +## See Also |
| 135 | + |
| 136 | +* [Grid SearchBox](slug:grid-searchbox) |
| 137 | +* [Grid OnRead](slug:components/grid/manual-operations) |
0 commit comments