зеркало из https://github.com/telerik/blazor-docs.git
3.2 KiB
3.2 KiB
title | description | type | page_title | slug | position | tags | ticketid | res_type |
---|---|---|---|---|---|---|---|---|
Set Programmatic Focus to a FormItem | How to set programmatic focus to an item in the Telerik Form. | how-to | How to set Programmatic Focus to a FormItem? How to set initial focus to a specific input in the TelerikForm? | form-kb-focus-formitem | telerik, blazor, ref | 1592333 | kb |
Environment
Product | Telerik UI for Blazor |
Description
How to set initial focus to a specific input in the TelerikForm
?
Solution
The <FormItem>
is an abstraction of the real editor that is rendered in the browser. To set programmatic focus you must:
- Use a [
FormItem
Template
]({%slug form-formitems-template%}) to provide the desired editor. - Use the
FocusAsync()
method accessible through the@ref
of the added editor.
caption Set programmatic focus to the first FormItem
@using System.ComponentModel.DataAnnotations
<TelerikForm Model="@person">
<FormValidation>
<DataAnnotationsValidator></DataAnnotationsValidator>
</FormValidation>
<FormItems>
<FormItem>
<Template>
<label for="first-name-textbox">First name:</label>
<TelerikTextBox @bind-Value="@person.FirstName"
@ref="@TextBoxReference"
Id="first-name-textbox">
</TelerikTextBox>
<TelerikValidationMessage For="(() => person.FirstName)"></TelerikValidationMessage>
</Template>
</FormItem>
<FormItem Field="@nameof(Person.LastName)" LabelText="Last name" Hint="Enter your last name" ColSpan="2"></FormItem>
<FormItem Field="@nameof(Person.DOB)" LabelText="Date of birth" Hint="Enter your Date of Birth"></FormItem>
</FormItems>
</TelerikForm>
@code {
private TelerikTextBox TextBoxReference { get; set; }
private Person person = new Person();
protected override async Task OnAfterRenderAsync(bool firstRender)
{
await Task.Delay(20);
await TextBoxReference.FocusAsync();
}
public class Person
{
[Editable(false)]
public int Id { get; set; }
[Required]
[MaxLength(20, ErrorMessage = "The first name should be maximum 20 characters long")]
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Required]
[MaxLength(25, ErrorMessage = "The last name should be maximum 25 characters long")]
[Display(Name = "Last Name")]
public string LastName { get; set; }
[Required]
[Display(Name = "Date of Birth")]
public DateTime? DOB { get; set; }
}
}
Notes
- The earliest reliable programmatic focus can occur in
OnAfterRenderAsync
and with some delay. The reason is thatOnAfterRenderAsync
fires when the DOM tree is built, but before the HTML output is actually rendered in the browser. After the event is fired, the .NET runtime sends the HTML to the browser. TheFocusAsync
method relies onJSInterop
, which in turn relies on the component to be rendered in the browser.