Skip to content

Commit 008ceab

Browse files
authored
Merge pull request #399 from telerik/focus-inputs-kb
kb(inputs): how to use FocusAsync
2 parents ffd614b + 282c5ea commit 008ceab

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

knowledge-base/inputs-set-focus.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
---
2+
title: Focus TextBox Programmatically
3+
description: How to focus a blazor textbox or input programmatically.
4+
type: how-to
5+
page_title: Focus textbox or input programmatically
6+
slug: inputs-kb-focus
7+
position:
8+
tags:
9+
ticketid: 1527317
10+
res_type: kb
11+
---
12+
13+
## Environment
14+
<table>
15+
<tbody>
16+
<tr>
17+
<td>Product Version</td>
18+
<td>2.25.0 and later</td>
19+
</tr>
20+
</tbody>
21+
</table>
22+
23+
## Description
24+
How do I focus a Blazor TelerikTextBox or any other input component programmatically?
25+
26+
## Solution
27+
The Telerik textboxes and inputs offer a `FocusAsync` method that lets you focus them with code.
28+
29+
The example below showcases it for a few of them, but it is available for all input components and buttons:
30+
31+
* AutoComplete
32+
* Button
33+
* ComboBox
34+
* CheckBox
35+
* DateInput
36+
* DatePicker
37+
* DateRangePicker
38+
* DateTimePicker
39+
* Editor
40+
* MaskedTextBox
41+
* MultiSelect
42+
* NumericTextBox
43+
* TextArea
44+
* TextBox
45+
* TimePicker
46+
47+
>caption Focus input with code
48+
49+
````CSHTML
50+
@code{
51+
async Task FocusTextbox()
52+
{
53+
await TextboxRef.FocusAsync();
54+
}
55+
56+
async Task FocusNumericTextbox()
57+
{
58+
await NumericTextboxRef.FocusAsync();
59+
}
60+
61+
async Task FocusDropdown()
62+
{
63+
await DropdownRef.FocusAsync();
64+
}
65+
}
66+
67+
<TelerikButton OnClick="@FocusTextbox">Focus textbox</TelerikButton>
68+
<TelerikButton OnClick="@FocusNumericTextbox">Focus numeric textbox</TelerikButton>
69+
<TelerikButton OnClick="@FocusDropdown">Focus dropdown</TelerikButton>
70+
71+
<TelerikTextBox @ref="@TextboxRef" @bind-Value="@TextoboxValue"></TelerikTextBox>
72+
<TelerikNumericTextBox @ref="@NumericTextboxRef" @bind-Value="@NumericTextoboxValue"></TelerikNumericTextBox>
73+
<TelerikDropDownList @ref="@DropdownRef" @bind-Value="@DropdownValue" Data="@DropdownData"></TelerikDropDownList>
74+
75+
@code{
76+
TelerikTextBox TextboxRef { get; set; }
77+
TelerikNumericTextBox<int> NumericTextboxRef { get; set; }
78+
TelerikDropDownList<string, string> DropdownRef { get; set; }
79+
80+
string TextoboxValue { get; set; } = "lorem ipsum";
81+
int NumericTextoboxValue { get; set; } = 123;
82+
string DropdownValue { get; set; } = "one";
83+
List<string> DropdownData { get; set; } = new List<string>() { "one", "two", "three" };
84+
}
85+
````
86+
87+
## Notes
88+
You can still use JavaScript to focus DOM elements by having a proper element reference or selector. The C# method is built on top of that. If you want more specific functionality (like <a href="https://feedback.telerik.com/blazor/1454982-always-highlight-all-numerictextbox-content-on-focus" target="_blank">selecting the text</a> as well, a pure JS solution might be simpler).

0 commit comments

Comments
 (0)