Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

415
Views
Container element not found in Blazor server side

I'm trying to embed an audio player using a javascript library called Wavesurfer.js in my Blazor component. I'm setting the IDs of the needed divs by generating GUIDs for them in the razor.cs page, then handing those values to the .razor component:

// WavePlayer.razor.cs
private Guid _mainDivGuid;
protected override async Task OnAfterRenderAsync(bool firstRender)
{
    if (firstRender)
    {
        _interop = new WavesurferJsInterop(_js, DotNetObjectReference.Create<WavePlayer>(this));

        if (_interop != null)
        {
            await _interop.Create(_mainDivGuid, Options);
            //preload file if URL already supplied
            if (Url != null)
            {
                await Load(Url);
            }
        }
    }
}

protected override void OnInitialized()
{
    _mainDivGuid = Guid.NewGuid();
}

// WavePlayer.razor
<div class="wavesurfer-ui-main" id="@_mainDivGuid"></div>

In the js interop it calls the actual wavesurfer.js where the Create method is invoked, and passes the GUID of the div where the player should get rendered.

When I run the component, I'm getting an error Error: Container element not found and I can confirm this in the js by adding a test for

if (!document.getElementById(mainDivGuid)) {
    console.log(mainDivGuid);
    console.log("No div element found for wavesurfer mainDivGuid"); // this gets logged
}

mainDivGuid is being created, it's the correct GUID, but by the time the Create method is invoked, the div apparently doesn't exist in the DOM.

Shouldn't using the OnAfterRenderAsync method ensure that the DOM is fully rendered before the Create gets called? I'm not sure how the wave player can be trying to fire its own events before the container exists!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You should use an ElementReference when you work between JS and .NET code. Something like below:

// WavePlayer.razor

@inject IJSRuntime Js

<div class="wavesurfer-ui-main" ref="@_mainDiv"></div>

@code {
    public ElementReference _mainDiv;

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            await Js.InvokeVoidAsync("Create", _mainDiv);
        }
    }
}

In the JS your function argument will be the DOM object.

function Create(thisIsTheDomObjectHere) {
  thisIsTheDomObjectHere.innerHTML("Hi there");
}

See more here:

https://blazor-university.com/javascript-interop/calling-javascript-from-dotnet/passing-html-element-references/

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!