{"record":{"id":"b48a7f2c0f8c1b7f","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 Element","messagePattern":"Named events may only be added as children of frames of type Element","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/294cab2f9b2e03af6b953820c7ab497c3c8b7ad9/src/Components/Components/src/Rendering/RenderTreeBuilder.cs#L665-L701","documentation":"Thrown by RenderTreeBuilder.AddNamedEvent (emitted by the @naming directive / named event feature for exposing element events under a callable name). A named event is attached to an HTML element so the framework can later dispatch to it; therefore the parent must be an Element frame.","triggerScenarios":"Calling builder.AddNamedEvent(eventType, assignedName) when GetCurrentParentFrameType() != Element — inside a Component, a Region, or at the root with no parent. In .razor, a @naming placed on a component or outside an element.","commonSituations":"Applying @naming to a component rather than an HTML element; hand-written RenderFragment that calls AddNamedEvent without an enclosing OpenElement; refactor that changed the enclosing tag from element to component.","solutions":["Place AddNamedEvent / @naming as a direct child of an element opened via OpenElement.","Verify the enclosing frame is an HTML element, not a component or region.","Move the @naming attribute onto the HTML element that owns the event (e.g., the <form> for onsubmit)."],"exampleFix":"// before\nbuilder.AddNamedEvent(\"onsubmit\", \"myForm\");\nbuilder.OpenElement(0, \"form\");\n...\nbuilder.CloseElement();\n\n// after\nbuilder.OpenElement(0, \"form\");\nbuilder.AddNamedEvent(\"onsubmit\", \"myForm\");\n...\nbuilder.CloseElement();","handlingStrategy":"validation","validationCode":"// Track element scope so AddNamedEvent is only called under an element.\nint _elDepth = 0;\nvoid Render(RenderTreeBuilder b) {\n    b.OpenElement(0, \"form\"); _elDepth++;\n    if (_elDepth > 0) b.AddNamedEvent(\"onsubmit\", \"myForm\");\n    _elDepth--; b.CloseElement();\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Put @naming on the HTML element that owns the event (e.g., <form @naming-submit=\"myForm\">).","Do not apply named events to components or regions.","Use the .razor @naming directive instead of manual AddNamedEvent where possible."],"tags":["blazor","rendering","rendertree","named-event","manual-renderfragment"],"analyzedSha":"294cab2f9b2e03af6b953820c7ab497c3c8b7ad9","analyzedAt":"2026-08-06T20:08:02.189Z","schemaVersion":2},"datasetVersion":"2026-08-06T23:17:07.152Z"}