New to Telerik UI for Blazor? Download free 30-day trial

Set Programmatic Focus to a FormItem


Product Telerik UI for Blazor


How to set initial focus to a specific input in the TelerikForm?


The <FormItem> is an abstraction of the real editor that is rendered in the browser. To set programmatic focus you must:

  1. Use a FormItem Template to provide the desired editor.
  2. Use the FocusAsync() method accessible through the @ref of the added editor.

Set programmatic focus to the first FormItem

@using System.ComponentModel.DataAnnotations

<TelerikForm Model="@person">
                <label for="first-name-textbox">First name:</label>
                <TelerikTextBox @bind-Value="@person.FirstName"
                <TelerikValidationMessage For="(() => person.FirstName)"></TelerikValidationMessage>
        <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>

@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
        public int Id { get; set; }

        [MaxLength(20, ErrorMessage = "The first name should be maximum 20 characters long")]
        [Display(Name = "First Name")]
        public string FirstName { get; set; }

        [MaxLength(25, ErrorMessage = "The last name should be maximum 25 characters long")]
        [Display(Name = "Last Name")]
        public string LastName { get; set; }

        [Display(Name = "Date of Birth")]
        public DateTime? DOB { get; set; }


  • The earliest reliable programmatic focus can occur in OnAfterRenderAsync and with some delay. The reason is that OnAfterRenderAsync 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. The FocusAsync method relies on JSInterop, which in turn relies on the component to be rendered in the browser.
In this article