DwsIFrameComponent
API ReferenceBasic
HTMLContent is rendered as the frame's document (its srcdoc). The frame fills its container; Title gives it an accessible name.
Show Code
IFrame.razor
<DwsIFrameComponent Name='FrameDemo_Basic' Title='Welcome note' HTMLContent='@_basicContent' Height='220' />IFrame.razor.cs
private const string _basicContent = @"<h2>Hello from the frame</h2>
<p>This document is rendered inside the frame. Its text and link colours follow the library theme.</p>
<p><a href=""#"">A link inside the frame</a></p>";Sandbox
A srcdoc frame inherits the page's origin, so an unsandboxed one runs its scripts with full access to the page. The frame is therefore sandboxed by default: Sandbox is AllowSameOrigin - scripts, forms, popups, modals and top-level navigation are blocked, while the theme can still reach the document. Lift individual restrictions with further flags; Unrestricted removes the sandbox. Allowing scripts together with the same origin lets the content lift its own sandbox, so that combination is logged as a warning.
Unrestricted
Default sandbox
Show Code
IFrame.razor
@* No sandbox: the script runs with full access to this page. *@
<DwsIFrameComponent Name='FrameDemo_Sandbox_Unrestricted' HTMLContent='@_untrustedContent' Sandbox='DwsIFrameSandbox.Unrestricted'
Height='160' BorderWidth='1' BorderColor='DwsColor.Error' />
@* Default (AllowSameOrigin): the script is blocked, the theme still applies. *@
<DwsIFrameComponent Name='FrameDemo_Sandbox_Default' HTMLContent='@_untrustedContent'
Height='160' BorderWidth='1' BorderColor='DwsColor.Success' />
@* Lift single restrictions with flags - e.g. forms and popups, still without scripts:
Sandbox='DwsIFrameSandbox.AllowSameOrigin | DwsIFrameSandbox.AllowForms | DwsIFrameSandbox.AllowPopups' *@IFrame.razor.cs
private const string _untrustedContent = @"<p id=""status"">No script ran.</p>
<p><a href=""javascript:alert('link')"">javascript: link</a></p>
<button onclick=""alert('handler')"">Inline handler</button>
<script>document.getElementById('status').textContent = 'A script ran - it could have read this page.';</script>";Sanitization
SanitizeCallback sees the content before the frame does; DwsIFrameComponent.SanitizeHtml is a ready-made one that strips scripts, event handlers and javascript: URLs. If the callback throws, the frame shows a short notice instead of the unsanitized content. The frames below are unrestricted, so the sanitizer is all that protects the page.
SanitizeHtml
Failing sanitizer
Show Code
IFrame.razor
<DwsIFrameComponent Name='FrameDemo_Sanitize_Default' HTMLContent='@_untrustedContent' SanitizeCallback='DwsIFrameComponent.SanitizeHtml'
Sandbox='DwsIFrameSandbox.Unrestricted' Height='160' BorderWidth='1' BorderColor='DwsColor.Success' />
<DwsIFrameComponent Name='FrameDemo_Sanitize_Failing' HTMLContent='@_untrustedContent' SanitizeCallback='FailingSanitizer'
Sandbox='DwsIFrameSandbox.Unrestricted' Height='160' BorderWidth='1' BorderColor='DwsColor.Warning' />IFrame.razor.cs
private const string _untrustedContent = @"<p id=""status"">No script ran.</p>
<p><a href=""javascript:alert('link')"">javascript: link</a></p>
<button onclick=""alert('handler')"">Inline handler</button>
<script>document.getElementById('status').textContent = 'A script ran - it could have read this page.';</script>";
// Any Func<string, string> works - e.g. a configured Ganss.Xss.HtmlSanitizer. One that throws fails closed.
private static string FailingSanitizer(string html) =>
throw new InvalidOperationException("The sanitizer service is unavailable.");Content-Security-Policy
ContentSecurityPolicy is written into the frame's document and can keep it off the network entirely - here the remote image is blocked. The frame is unrestricted, so the policy is all that keeps the content from loading.
Content-Security-Policy
Show Code
IFrame.razor
<DwsIFrameComponent Name='FrameDemo_Csp' HTMLContent='@_trackingContent' ContentSecurityPolicy='@_offlinePolicy'
Sandbox='DwsIFrameSandbox.Unrestricted' Height='160' BorderWidth='1' BorderColor='DwsColor.Info' />IFrame.razor.cs
private const string _trackingContent = @"<p>A remote image below:</p>
<img src=""/images/blackhole_black.png"" alt=""Blocked by the policy"" width=""64"">";
// Nothing may be loaded from the network; inline styles stay allowed so the library theme still applies.
private const string _offlinePolicy = "default-src 'none'; img-src data:; style-src 'unsafe-inline'";Size, border and scrolling
Width and Height take pixels. BorderWidth draws a border in BorderColor, resolved through the theme. Scrolling lets a document taller than the frame scroll: the first frame scrolls, the second cuts the document off.
Show Code
IFrame.razor
<DwsIFrameComponent Name='FrameDemo_Sizing_Scrolling' HTMLContent='@_longContent' Width='320' Height='180'
BorderWidth='2' BorderColor='DwsColor.Primary' Scrolling='true' />
<DwsIFrameComponent Name='FrameDemo_Sizing_Clipped' HTMLContent='@_longContent' Width='320' Height='180'
BorderWidth='2' BorderColor='DwsColor.Secondary' />IFrame.razor.cs
private static readonly string _longContent =
string.Concat(Enumerable.Range(1, 12).Select(i => $"<p>Paragraph {i} of a document taller than its frame.</p>"));Disabled
A disabled frame is inert: its document cannot be clicked, scrolled or tabbed into.
Show Code
IFrame.razor
<DwsSwitch Name='FrameDemo_Events_Disabled' Label='Disabled' @bind-Value='@_disabled' Class='mb-2' />
<DwsIFrameComponent Name='FrameDemo_Disabled' HTMLContent='@_disabledContent' Disabled='@_disabled' Height='160' BorderWidth='1' />IFrame.razor.cs
private bool _disabled;
private const string _disabledContent = "<p>Content version 1.</p><p><a href='#'>Try to click or tab to this link</a></p>";Interface-Description
| Name | Typ | Parameters | Description |
|---|---|---|---|
| SanitizeHtml | static string | A sanitizer for SanitizeCallback built on HtmlSanitizer's default allow-list: removes scripts, event-handler attributes, javascript: URLs, nested frames, <style>/<link> elements and everything else outside it. Pass a callback over a configured HtmlSanitizer when the content needs more. |
| Name | Typ | Default | Description |
|---|---|---|---|
| HTMLContent | string | string.Empty | The HTML document rendered in the frame (its srcdoc). Passed through SanitizeCallback first when one is set. Unsandboxed, the document runs in the page's own origin. |
| Src | string? | null | Address of an external document to embed (its src); takes precedence over HTMLContent. Third-party embeds usually need a Sandbox that allows scripts. ApplyTheme does not reach another origin. |
| RequireConsent | bool | false | Two-click embedding (GDPR) for Src: a placeholder names the site and offers "Load content"; nothing is requested before. Under static SSR the button is a link with ?dws-embed={Name}. |
| ConsentContent | RenderFragment? | null | Replaces the consent placeholder's default text; the button stays. |
| SanitizeCallback | Func<string, string>? | null | Applied to HTMLContent before it reaches the frame; its result is rendered instead. Runs only when the content or the callback changes. If it throws, none of the content is rendered - the frame shows a localized notice and the error is logged. DwsIFrameComponent.SanitizeHtml is a ready-made callback. |
| Sandbox | DwsIFrameSandbox | DwsIFrameSandbox.AllowSameOrigin | The restrictions lifted from the frame's sandbox, as flags (AllowScripts, AllowForms, AllowPopups, ...). The default blocks scripts, forms, popups, modals and top-level navigation while the theme can still reach the document. None applies every restriction; Unrestricted removes the sandbox attribute. Takes precedence over a sandbox passed through AdditionalAttributes. AllowScripts together with AllowSameOrigin lets the content lift its own sandbox and is logged as a warning. |
| ContentSecurityPolicy | string? | null | A Content-Security-Policy for the frame's document, written as a meta element ahead of the content (after a doctype), and after the sanitizer. Content can tighten it but never loosen it. ApplyTheme injects a style element, so a policy restricting style-src needs 'unsafe-inline' for the theme to apply. |
| ApplyTheme | bool | true | Passes the library theme into the frame's document: the palette variables (--text, --link, --surface, --primary, ...) become available under the same names, and the default text, link colour and font follow them, also after a theme switch. The content's own styles win. Needs AllowSameOrigin in Sandbox (the default) - otherwise the document cannot be reached. |
| Title | string? | null | Accessible name of the frame. Falls back to a localized "Embedded content". |
| Scrolling | bool | false | Whether the frame's document can scroll. |
| Width | int? | null | Width in pixels. Unset, the frame fills its container. |
| Height | int? | null | Height in pixels. Unset, the frame fills its container's height. |
| BorderWidth | int? | null | Border width in pixels. No border when unset. |
| BorderColor | DwsColor | DwsColor.Undefined | Border colour, resolved through the theme's CSS variables. Undefined uses the theme's secondary-bright. Only visible with a BorderWidth. |
| Name | Typ | Description |
|---|---|---|
| OnLoad | EventCallback | Raised each time the frame's document has finished loading - on first render and whenever HTMLContent changes. |
| Name | Typ | Default | Description |
|---|---|---|---|
| Element | ElementReference | The associated web component. May be null if accessed before the component is rendered. | |
| ComponentGuid | Guid | Unique identifier for the component. | |
| TypeString | string | The name of the component type, obtained from reflection. | |
| Authorized_RegisterEnabled | bool | false | Indicates whether this component is enabled through a permission registry. |
| Authorized_RegisterSelected | bool | false | Indicates whether this component is selected in permission selection mode. |
| GetInitializedState | bool | false | Returns whether the component is initialized. |
| GetRenderModeState | DwsRenderMode | Returns the current render mode. | |
| GetComponentState | DwsComponentStateType | Returns the current component state. | |
| GetIsInteractiveState | bool | false | Returns whether the component is interactive. |
| GetIsPrerenderingState | bool | false | Returns whether the component is prerendering. |
| GetComponentLoadingState | bool | false | Returns whether the component is loading. |
| IsFormBusy | bool | false | Whether the DwsForm (or DwsFormBusyRegion) around the component is locked because its submit handler is running. The state itself is the cascaded FormBusyState (IDwsFormBusyState). |
| Name | Typ | Parameters | Description |
|---|---|---|---|
| DebugInfo | void | Logs debug information about the component. | |
| UpdateState | void | Updates the component's state and triggers StateHasChanged. | |
| UpdateStateAsync | Task | Asynchronously updates the component's state and triggers StateHasChanged. | |
| Dispose | void | Disposes the component and releases resources. | |
| FocusAsync | ValueTask | bool containedInScrollableList | Sets focus to the component's element. |
| Name | Typ | Default | Description |
|---|---|---|---|
| Name | string | Required unique identifier for the component within its container. | |
| Visible | bool | true | Determines if the component is visible in the UI. |
| Disabled | bool | false | Makes the frame inert: its document cannot be clicked, scrolled or reached with the keyboard. |
| ComponentNameSpace | string | The namespace for permission-based access control. | |
| PermissionKey | string? | null | A stable key that takes the place of Name in the generated permission namespace (Page.PermissionKey): the stored permission survives a rename, and components that share a key share one permission entry. Null: keyed by Name. Read once, when the component registers; an explicit ComponentNameSpace wins. |
| Class | string | Optional CSS class names. | |
| Style | string | Optional in-line styles. | |
| Tag | object | An optional user-defined object to store any additional data with the component. | |
| BackReference | DwsReference | A reference to the enclosing component. | |
| ID | string | The HTML ID attribute for the component. | |
| AdditionalAttributes | Dictionary<string, object> | Additional HTML attributes for the <iframe> element itself - e.g. allow, loading or referrerpolicy, which override the defaults loading="lazy" and referrerpolicy="no-referrer". class, title, sandbox, srcdoc, scrolling and inert are controlled by the component. Class, Style and ID go on the wrapper around it. | |
| IgnorePermissionChecks | bool | false | Bypasses all permission checks for this component. |
| IgnorePermissionSelection | bool | false | Ignore permission selection mode for this component. |
| IgnoreFormBusy | bool | false | Takes the component out of the submit lock of the DwsForm (or DwsFormBusyRegion) around it: IsFormBusy stays false for it. |
| Name | Typ | Description |
|---|---|---|
| OnInitializedCallbackStarted | EventCallback | Callback when OnInitialized starts. |
| OnInitializedCallback | EventCallback | Callback during OnInitialized. |
| OnInitializedCallbackCompleted | EventCallback | Callback when OnInitialized completes. |
| OnInitializedAsyncCallbackStarted | EventCallback | Callback when OnInitializedAsync starts. |
| OnInitializedAsyncCallback | EventCallback | Callback during OnInitializedAsync. |
| OnInitializedAsyncCallbackCompleted | EventCallback | Callback when OnInitializedAsync completes. |
| OnIsComponentLoadingChanged | EventCallback<bool> | Callback when IsComponentLoading changes. |
| OnCurrentRenderModeChanged | EventCallback<DwsRenderMode> | Callback when CurrentRenderMode changes. |
| OnComponentStateChanged | EventCallback<DwsComponentStateType> | Callback when ComponentState changes. |
