FocusAsync() using RadzenTextBox

Referring to post:

Hi

If I want to set focus to a RadzenTextBox when a dialog in a CRUD is open, would I set the code OnAfterRenderAsync? I tried this but a label on a Tab within which the textbox exists is getting focus. What am I doing wrong?

If I put the same code within a click event on a button, it's fine!!

Yes.

How is the TextBox inside a Label? We will need more info about your page.

Thank you.

I'll try again and see if I can recreate the issue and get back to you

To recreate, I created a CRUD page with to fields FirstName & LastName. Without anything specific, when the Add page is displayed, FirstName by default has focus.

If using the OnPreRender, I try and focus the LastName
using, await LastName.Element.FocusAsync();
I can see that when the Add page loads, the LastName for a moment shows the cursor in the LastName and then focus goes to FirstName. So doesn't work as expected.

Now if I enclose the 2 textboxes in a Tab, the tab has focus. Can't get focus on LastName textbox.

Hope this helps you recreate the issue.

What is OnPreRender? Show us some actual code.

Sorry, that should be OnAfterRenderAsync.

Here's the AddAuthor Razor:

Here's the codebehind:
public partial class AddAuthorComponent
{
protected RadzenTextBox LastName;

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            await LastName.Element.FocusAsync();
        }
    }

}

Have you tried unchecking the "Automatically focus first element" option in the Open dialog action?

Thank you so much.

I kept looking on the Add page seeing that it was focusing on the intended textbox and then focusing on the first field.

Now amended at the dialog and it works like a charm.

Once again, thank you for your assistance.