OnInitializedAsync getting fired multiple times

Posted 1 year ago by Laz_SRP
0

I'm trying to use OnInitializedAsync for initializing values but when I'm changing those values in 3rd party controls and clicking a button in a Telerik grid that calls a method in my code-behind, OnInitializedAsync is getting fired multiple times which re-initializes variables that I don't want re-initialized.  I'm having trouble figuring out what to google just to research why this is happening.  This is not an issue where OnInitializedAsync is fired twice because render-mode="ServerPrerendered" - even if I change it to just "Server" I'm still seeing OnInitializedAsync fired multiple times after the razor page has already initially loaded.

How do I ensure that OnInitializedAsync is only fired on the initial page load?

  • 0

    OnInitializedAsync is essentially a constructor in blazor. It should only be fired once. Try adding something like:

    protected override async Task OnInitializedAsync()
    {
        //.... your other code
        Console.WriteLine("Component Initialized");
    }    

    Then watch your browser console (if WebAssembly) or command line (if Blazor Server) to see how many times "Component Initialized" is printed out.

    If it is printing out multiple times, then maybe the Telerik grid is somehow forcing a full page reload?

    Posted 1 year ago by selliott Edited 1 year ago
  • 1

    It was in the grid, the fix was easy - needed to change ButtonType="ButtonType.Submit" to ButtonType="ButtonType.Button"

    Posted 1 year ago by Laz_SRP
  • 0

    For me, it happened because of clicking on a <a> tag with href="#"

    Posted 47 days ago by samberk
Someone is typing...

Post a Reply

You must be logged in to add a new post.
Number of online users: 0
An error has occurred. This application may no longer respond until reloaded. Reload 🗙