When you set the Window's Visible attribute to true and the popup renders, the Blazor framework will fire the OnAfterRenderAsync event.
The OnAfterRenderAsync handler is the place to focus the desired textbox. Note that you will need a small delay ( await Task.Delay(300) ) to wait for the textbox JavaScript instance to initialize.
The OnAfterRenderAsync event fires a lot of times during the page life cycle. You will need a boolean flag to detect when to focus the textbox (e.g. FocusFlag ). Set this flag to true when you set the Window Visible attribute to true.
The following snippet demonstrates the above instructions. I also created this REPL example that you can use as reference.
Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.
Thanks for the update. Above is for the button click event. I need for Page Intialization and I have tried for Page Intialization and it's not working. Could please update on the same.
The required 300ms delay seems like an unfortunate workaround. I would love to be able to focus elements without a noticeable pause. While I could likely shorten the delay (100-200ms) ... the best answer would require a solution that doesn't require delaying at all. Is there anything in the backlog tracking why this is necessary?
Dimo
Telerik team
commented on 06 Dec 2023, 01:47 PM
@Karl - The Blazor framework doesn't expose events for components to know when they are actually rendered on the page. So you either need to track the HTML DOM tree in the browser with JavaScript MutationObserver, or use magic numbers for a delay. The second approach is a lot easier to implement, hence we suggest it.