UserControlHost – More trouble than it’s worth?

This week, I’m battling the UserControlHost page type in AL. On paper, well, on screen, this is a great page type for many of the things I create. But it has a few quirks, enough to make me reconsider the choice. Check out the video:

https://youtu.be/1j0M4lHtrbQ

In this video, Erik explores the UserControlHost page type in Business Central — introduced in runtime 15 — and walks through the frustrating quirks that make it harder to use than expected. He demonstrates the issues with captions, the broken OnClosePage trigger, and experiments with alternative page types to find a workable solution.

What Is the UserControlHost Page Type?

The UserControlHost page type was introduced in Business Central runtime 15. According to Microsoft’s documentation, it’s a specialized page type designed to render a single user control in the client, optimizing the layout to maximize the available space for the control. It’s particularly useful for embedding Power BI reports or displaying custom JavaScript-based UIs.

Unlike standard pages, the UserControlHost page type has strict limitations:

  • Only one user control is allowed in the layout
  • Extensibility is restricted
  • Actions are limited
  • Only three triggers are available: OnInit, OnOpenPage, and OnClosePage

On the surface, this sounds perfect — a full-page canvas for your JavaScript control. But as Erik discovers, there are some annoying quirks that undermine its usefulness.

The “Add-in” Caption Problem

The first issue Erik encounters is the mysterious “Add-in” label that appears in the page header alongside whatever caption you’ve set. When you create a UserControlHost page with a caption like “Hello World,” the header displays both “Hello World” and “Add-in.” Why does it need to say “Add-in”? It’s a strange, unwanted label.

Erik experiments with several approaches to get rid of it:

Removing the Caption Entirely

If you remove the caption property, the “Add-in” text disappears — but now the page name itself shows up in the header instead. So you trade one unwanted label for another.

Naming the Page a Space

Setting the page name to a single space character does compile and gets close to a clean header. But you can’t realistically ship a page named with a space character, especially not in an AppSource app.

Using Non-Breaking Spaces and Special Characters

Erik tries using a non-breaking space (  / Unicode U+00A0) and other whitespace characters in the caption. None of them work — Microsoft’s whitespace trimming catches them all, and the “Add-in” label returns.

Setting the Caption Programmatically

Another attempt: setting CurrPage.Caption to blank in the OnOpenPage or OnInit triggers. Same result — “Add-in” comes right back.

trigger OnOpenPage()
begin
    CurrPage.Caption := '';
end;

No matter what approach is tried, the “Add-in” label stubbornly persists.

The Broken OnClosePage Trigger

The second — and far more serious — issue is with the OnClosePage trigger. Of the three available triggers on a UserControlHost page (OnInit, OnOpenPage, OnClosePage), the close trigger is critical for scenarios where you need to save user data before the page is dismissed.

First, there’s no OnQueryClosePage trigger at all. The compiler explicitly blocks it:

“The trigger OnQueryClosePage cannot be used if the property PageType of the page is set to UserControlHost.”

So you can’t prevent a user from closing the page. But at least you should be able to react to the close and save data, right? Erik adds a Confirm dialog to the OnClosePage trigger to test this:

trigger OnClosePage()
begin
    if Confirm('Save?') then
        Message('Saved.');
end;

When he clicks the close button, the page just closes immediately — the trigger never fires. Erik confirms he’s running the latest available version at the time of recording, so this isn’t an old-version bug.

This is a major problem. Erik’s real-world use case is a spreadsheet control add-in that lives inside a UserControlHost page. Without a working close trigger, there’s no way to save user data when someone hits Escape or closes the page. Everything is just lost.

Exploring Alternative Page Types

Given these issues, Erik explores what happens when you abandon UserControlHost and use other page types instead. Here’s the source code he’s working with for the alternative approach:

page 50112 "Test"
{
    PageType = Card;
    SourceTable = Customer;
    Editable = false;
    UsageCategory = None;
    ApplicationArea = all;
    layout
    {
        area(Content)
        {
            usercontrol(test; fullpagetest)
            {
            }
        }
    }
    actions
    {
        area(Processing)
        {
            action(Test2)
            {
                Caption = 'Test';
                trigger OnAction()
                begin
                    message('hello');
                end;
            }
        }
    }
}

The control add-in definition and startup script handle the sizing:

controladdin fullpagetest
{
    StartupScript = 'startup.js';
    VerticalStretch = true;
}
// startup.js
container = document.getElementById("controlAddIn");
container.style.background = "red";

var heightindcator = window.frameElement.closest('div[class~="control-addin-form"]');
window.frameElement.style.height = (heightindcator.offsetHeight - 5).toString() + 'px';
window.frameElement.style.maxHeight = (heightindcator.offsetHeight - 5).toString() + 'px';

window.addEventListener('resize', function (event) {
    var heightindcator = window.frameElement.closest('div[class~="control-addin-form"]');
    window.frameElement.style.height = (heightindcator.offsetHeight - 5).toString() + 'px';
    window.frameElement.style.maxHeight = (heightindcator.offsetHeight - 5).toString() + 'px';
}, true);

The startup script manually calculates and sets the iframe height to fill the available space, and re-calculates on window resize — essentially replicating what UserControlHost is supposed to give you for free.

Card Page Type

Using PageType = Card removes the “Add-in” label, which is a win. However, it introduces a horizontal rule (line) in the layout. The caption behaves normally — you can set it or hide it as expected. Most importantly, the OnClosePage and OnQueryClosePage triggers work properly.

List Page Type

A list page still renders the horizontal line and adds a “View” element. It also renders differently in the Role Center. The control add-in ends up positioned below the line, which is visually awkward. The user control can also shift to the bottom of the screen depending on how copy-paste of the page definition goes.

Document Page Type

With PageType = Document, you don’t get a full-page experience. The caption displays correctly, but the horizontal line persists. There’s also too much “baggage” with a Document page type — it expects FastTabs, FactBoxes, and other structural elements that don’t make sense for a pure user control scenario.

StandardDialog Page Type

The StandardDialog page type actually renders the control perfectly — no “Add-in” label, clean layout, proper sizing. However, it comes with OK and Cancel buttons at the bottom, which makes it unsuitable as a general-purpose full-page host. It is a dialog, after all.

Summary of Issues

  1. Persistent “Add-in” label: The UserControlHost page type always displays “Add-in” in the header. No amount of caption manipulation — blank values, special whitespace characters, or programmatic changes — can remove it.
  2. Non-functional OnClosePage trigger: The OnClosePage trigger doesn’t appear to fire, meaning you cannot save data or prompt the user when the page is closed.
  3. No OnQueryClosePage support: You cannot prevent the user from closing the page at all. Combined with the broken OnClosePage, this means data loss is virtually guaranteed in editing scenarios.
  4. Alternative page types all have trade-offs: Card and List pages give you working triggers but introduce visual artifacts. Document pages carry too much structural overhead. StandardDialog renders beautifully but adds OK/Cancel buttons.

Conclusion

The UserControlHost page type in Business Central has the right idea — a dedicated full-page canvas for JavaScript control add-ins. But in practice, the inability to control the caption properly and the broken close triggers make it problematic for real-world scenarios, especially any use case involving user-editable data. Until Microsoft addresses these issues, developers may need to fall back to a Card page type with manual sizing via JavaScript (as shown in the startup script above), accepting the visual trade-off of the horizontal line in exchange for working triggers and proper lifecycle management. If you’ve found a workaround for any of these issues, Erik would love to hear about it in the comments.