How can I show a Notification at startup when using InteractiveServer

1 Answer 14 Views
Notification
Martin Herløv
Top achievements
Rank 2
Bronze
Iron
Iron
Martin Herløv asked on 30 Jul 2025, 09:00 AM
I want to show an error if data load fails in OnInitializedAsync. I will save the message in a variable and show it in OnAfterRenderAsync or OnAfterRender.

The toast work when the page is fully loaded and I press the button
I am very open for suggestion on showing messages in another way

I have tried all the life cycle handler but in theory this should work.


public partial class TelerikTest
{
    [Inject] public IJSRuntime JsRuntime { get; set; }
    [Inject] public NavigationManager Navigation { get; set; }
    private TelerikNotification NotificationReference { get; set; }

    protected override void OnAfterRender(bool firstRender)
    {
        if (firstRender && Navigation.Uri.StartsWith("https"))
        {
            StateHasChanged();
            ShowErrorNotification();
        }
    }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender && Navigation.Uri.StartsWith("https"))
        {
            await JsRuntime.InvokeVoidAsync("console.log", "Component rendered!");
            StateHasChanged();
        }
    }
    
    private void ShowErrorNotification()
    {
        NotificationReference.Show(new NotificationModel
        {
            Text = "An error has occurred!",
            ThemeColor = ThemeConstants.Notification.ThemeColor.Error,
            Closable = true,
            CloseAfter = 20000
        });
    }
}



Martin Herløv
Top achievements
Rank 2
Bronze
Iron
Iron
commented on 31 Jul 2025, 05:41 PM

I ended up using the modal TelerikWindow

1 Answer, 1 is accepted

Sort by
0
Dimo
Telerik team
answered on 01 Aug 2025, 06:01 AM

Hi Martin,

Your code works on my side. A potential cause for the unexpected behavior is that the failure in OnInitializedAsync occurs after OnAfterRender. Based on what you say, this is very likely.

The phenomenon should become evident with some debugging or Console.WriteLine.

<TelerikNotification @ref="@NotificationReference" />

@code {
    private TelerikNotification? NotificationReference { get; set; }

    private bool LoadingFailed { get; set; }

    protected override async Task OnInitializedAsync()
    {
        await LoadData();

        await base.OnInitializedAsync();
    }

    private async Task LoadData()
    {
        // The method execution duration determines from where the Notification will be displayed.
        await Task.Delay(1000);

        LoadingFailed = true;
        Console.WriteLine("Loading Failed");

        if (NotificationReference is not null)
        {
            ShowErrorNotification("From LoadData");
        }
    }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        Console.WriteLine("Loading Failed Check");
        if (firstRender && LoadingFailed)
        {
            LoadingFailed = false;
            ShowErrorNotification("From OnAfterRenderAsync");
        }

        await base.OnAfterRenderAsync(firstRender);
    }

    private void ShowErrorNotification(string message)
    {
        NotificationReference?.Show(new NotificationModel
        {
            Text = message,
            ThemeColor = ThemeConstants.Notification.ThemeColor.Error,
            Closable = true,
            CloseAfter = 20000
        });
    }
}

 

Regards,
Dimo
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
Notification
Asked by
Martin Herløv
Top achievements
Rank 2
Bronze
Iron
Iron
Answers by
Dimo
Telerik team
Share this question
or