dotnet/aspnetcore · error · InvalidOperationException

Named events may only be added as children of frames of type

Error message

Named events may only be added as children of frames of type {RenderTreeFrameType.Element}

What it means

AddNamedEvent was called when the current parent frame is not an Element. Named events (the framework's mechanism for exposing element events by application-assigned name, e.g. form 'onsubmit' for streaming validation) must be a child of an Element frame.

Solutions

  1. Call AddNamedEvent only inside an OpenElement scope.
  2. Reorder so OpenElement(...) precedes AddNamedEvent.
  3. Confirm the named event targets an HTML element, not a component.

Example fix

// before
builder.AddNamedEvent("onsubmit", "myform");
builder.OpenElement(0, "form");

// after
builder.OpenElement(0, "form");
builder.AddNamedEvent("onsubmit", "myform");
Defensive patterns

Strategy: validation

Validate before calling

if (currentParentFrameType != RenderTreeFrameType.Element)
    throw new InvalidOperationException("Open an element before AddNamedEvent.");
builder.AddNamedEvent(eventType, assignedName);

Type guard

static bool ParentIsElementForNamedEvent(RenderTreeFrameType t) => t == RenderTreeFrameType.Element;

Prevention

When it happens

Trigger: Calling builder.AddNamedEvent(eventType, assignedName) at the root, inside a Component, or inside a Region without an enclosing element. Generated named-event code attached to a component tag.

Common situations: Custom EditForm/streaming-validation integrations emitting named events against the wrong scope. Source generator bugs. Manual tree builders misordering named-event frames.

Related errors


AI-assisted analysis of dotnet/aspnetcore@3600ca084e (2026-08-11). Data as JSON: /api/errors/68e70319ce8a8a19. Report an issue: GitHub.

Appendix: source

Thrown at src/Components/Components/src/Rendering/RenderTreeBuilder.cs:683

    }

    /// <summary>
    /// Assigns a name to an event in the enclosing element.
    /// </summary>
    /// <param name="eventType">The event type, e.g., 'onsubmit'.</param>
    /// <param name="assignedName">The application-assigned name.</param>
    public void AddNamedEvent(string eventType, string assignedName)
    {
        ArgumentNullException.ThrowIfNull(eventType);
        ArgumentException.ThrowIfNullOrEmpty(assignedName);

        // Note that we could trivially extend this to a generic concept of "named values" that exist within the rendertree
        // and are tracked when added, removed, or updated. Currently we don't need that generality, but if we ever do, we
        // can replace RenderTreeFrameType.NamedEvent with RenderTreeFrameType.NamedValue and use it to implement named events.

        if (GetCurrentParentFrameType() != RenderTreeFrameType.Element)
        {
            throw new InvalidOperationException($"Named events may only be added as children of frames of type {RenderTreeFrameType.Element}");
        }

        _entries.AppendNamedEvent(eventType, assignedName);
        _lastNonAttributeFrameType = RenderTreeFrameType.NamedEvent;
    }

    /// <summary>
    /// Appends a frame representing a region of frames.
    /// </summary>
    /// <param name="sequence">An integer that represents the position of the instruction in the source code.</param>
    public void OpenRegion(int sequence)
    {
        // We are entering a new scope, since we track the "duplicate attributes" per
        // element/component we might need to clean them up now.
        if (_hasSeenAddMultipleAttributes)
        {
            var indexOfLastElementOrComponent = _openElementIndices.Peek();
            ProcessDuplicateAttributes(first: indexOfLastElementOrComponent + 1);

View on GitHub (pinned to 3600ca084e)