how to call child component method from parent component in blazor?

First you need to capture a reference of your child component: <ChildComponent @ref=”child” /> Then you can use this reference to call the child’s component methods as you do in your code. <button onClick=”@ShowModal”>show modal</button> @code{ ChildComponent child; void ShowModal(){ child.Show(); } } The namespace of your component need to be added by a using … Read more

Blazor client-side debugging

For those who like some pictures, here’s a step by step using Visual Studio 16.4 preview (.NET Core 3.1 preview 2) & Chrome version 78. Start up the app using a debug profile. E.g. After site loads, and with the cursor focus on the chrome tab press “Shift+Alt+D”. Chrome will open a new tab showing … Read more

Blazor, how can I trigger the enter key event to action a button function?

onkeypress is fired only for character keys. onkeydown will fire for all keys pressed. I found some explanation of differences between all key events here Try it with onkeydown and it worked: <input type=”text” @onkeydown=”@Enter” /> In the event handler you will have to do this (notice that I check for both Enter and NumpadEnter … Read more

Blazor redirect to login if user is not authenticated

There may be slicker ways of doing this, but this is what worked for me: Assuming you’ve configured Authentication correctly according to these instructions In your MainLayout.razor (which is used by default for all components) add a code block as follows: @inject NavigationManager NavigationManager @code{ [CascadingParameter] protected Task<AuthenticationState> AuthStat { get; set; } protected async … Read more

StateHasChanged() vs InvokeAsync(StateHasChanged) in Blazor

I have tried calling StateHasChanged() – instead of InvokeAsync(StateHasChanged) – in a Timer’s Elapsed event, and it works as expected That must have been on WebAssembly. When you try that on Blazor Serverside I would expect an exception. StateHasChanged() checks if it runs on the right thread. The core issue is that the rendering and … Read more

When should I call StateHasChanged and when Blazor automatically intercepts that something is changed?

Generally speaking, the StateHasChanged() method is automatically called after a UI event is triggered, as for instance, after clicking a button element, the click event is raised, and the StateHasChanged() method is automatically called to notify the component that its state has changed and it should re-render. When the Index component is initially accessed. The … Read more