{"record":{"id":"68e70319ce8a8a19","repo":"dotnet/aspnetcore","slug":"named-events-may-only-be-added-as-children-of-fram","errorCode":null,"errorMessage":"Named events may only be added as children of frames of type {RenderTreeFrameType.Element}","messagePattern":"Named events may only be added as children of frames of type (.+?)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Components/Components/src/Rendering/RenderTreeBuilder.cs","lineNumber":683,"sourceCode":"    }\n\n    /// <summary>\n    /// Assigns a name to an event in the enclosing element.\n    /// </summary>\n    /// <param name=\"eventType\">The event type, e.g., 'onsubmit'.</param>\n    /// <param name=\"assignedName\">The application-assigned name.</param>\n    public void AddNamedEvent(string eventType, string assignedName)\n    {\n        ArgumentNullException.ThrowIfNull(eventType);\n        ArgumentException.ThrowIfNullOrEmpty(assignedName);\n\n        // Note that we could trivially extend this to a generic concept of \"named values\" that exist within the rendertree\n        // and are tracked when added, removed, or updated. Currently we don't need that generality, but if we ever do, we\n        // can replace RenderTreeFrameType.NamedEvent with RenderTreeFrameType.NamedValue and use it to implement named events.\n\n        if (GetCurrentParentFrameType() != RenderTreeFrameType.Element)\n        {\n            throw new InvalidOperationException($\"Named events may only be added as children of frames of type {RenderTreeFrameType.Element}\");\n        }\n\n        _entries.AppendNamedEvent(eventType, assignedName);\n        _lastNonAttributeFrameType = RenderTreeFrameType.NamedEvent;\n    }\n\n    /// <summary>\n    /// Appends a frame representing a region of frames.\n    /// </summary>\n    /// <param name=\"sequence\">An integer that represents the position of the instruction in the source code.</param>\n    public void OpenRegion(int sequence)\n    {\n        // We are entering a new scope, since we track the \"duplicate attributes\" per\n        // element/component we might need to clean them up now.\n        if (_hasSeenAddMultipleAttributes)\n        {\n            var indexOfLastElementOrComponent = _openElementIndices.Peek();\n            ProcessDuplicateAttributes(first: indexOfLastElementOrComponent + 1);","sourceCodeStart":665,"sourceCodeEnd":701,"githubUrl":"https://github.com/dotnet/aspnetcore/blob/3600ca084e9c8b5f4174fc5e747f4c52d2100806/src/Components/Components/src/Rendering/RenderTreeBuilder.cs#L665-L701","documentation":"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.","triggerScenarios":"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.","commonSituations":"Custom EditForm/streaming-validation integrations emitting named events against the wrong scope. Source generator bugs. Manual tree builders misordering named-event frames.","solutions":["Call AddNamedEvent only inside an OpenElement scope.","Reorder so OpenElement(...) precedes AddNamedEvent.","Confirm the named event targets an HTML element, not a component."],"exampleFix":"// before\nbuilder.AddNamedEvent(\"onsubmit\", \"myform\");\nbuilder.OpenElement(0, \"form\");\n\n// after\nbuilder.OpenElement(0, \"form\");\nbuilder.AddNamedEvent(\"onsubmit\", \"myform\");","handlingStrategy":"validation","validationCode":"if (currentParentFrameType != RenderTreeFrameType.Element)\n    throw new InvalidOperationException(\"Open an element before AddNamedEvent.\");\nbuilder.AddNamedEvent(eventType, assignedName);","typeGuard":"static bool ParentIsElementForNamedEvent(RenderTreeFrameType t) => t == RenderTreeFrameType.Element;","tryCatchPattern":null,"preventionTips":["Open an element scope before AddNamedEvent.","Confirm named events target HTML elements (e.g. <form>), not components."],"tags":["blazor","render-tree-builder","named-event","frame-type","scope"],"backgroundTag":null,"analyzedSha":"3600ca084e9c8b5f4174fc5e747f4c52d2100806","analyzedAt":"2026-08-11T16:32:30.678Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}