Blazor is just a replacement (more precisely, "adding a value") with javascript. This solution is only for the client side (but may be with some easy connections with ASP.Net in the future).
However, it is entirely based on html and CSS. C # replaces part of js with web build. This way, nothing has changed in the way you access / modify the html controls.
At the moment (version 0.1.0) you should rely on the HTML DOM focus() method to do what you intend to do (yes, you should use javascript at the moment :().
// Not tested code // This is JavaScript. // Put this inside the index.html. Just below <script type="blazor-boot"></script> <script> Blazor.registerFunction('Focus', (controlId) => { return document.getElementById(controlId).focus(); }); </script> //and then wrap it for calls from .NET: // This is C# public static object Focus(string controlId) { return RegisteredFunction.Invoke<object>("Focus", controlId); //object type is used since Invoke does not have a overload for void methods. Don't know why. //this will return undifined according to js specs }
see below for more information.
If you want to improve pashaging js neatly . you can do something like that. fooobar.com/questions/1276054 / ...
public class BlazorExtensionScripts : Microsoft.AspNetCore.Blazor.Components.BlazorComponent { protected override void BuildRenderTree(Microsoft.AspNetCore.Blazor.RenderTree.RenderTreeBuilder builder) { builder.OpenElement(0, "script"); builder.AddContent(1, "Blazor.registerFunction('Focus', (controlId) => { document.getElementById(controlId).focus(); });"); builder.CloseElement(); } public static void Focus(string controlId) { RegisteredFunction.Invoke<object>("Focus", controlId); } }
then add this component to the root directory. (App.cshtml)
<BlazorExtensionScripts></BlazorExtensionScripts> <Router AppAssembly=typeof(Program).Assembly />
source share