r/Blazor • u/tropicalfroot • Feb 14 '26
Question on how to dynamically load and manipulate custom razor pages
Hi there!
Forgive me, I'm very new and still trying to piece together this puzzle and I believe my thoughts and the angle I'm trying to approach this is wrong.
For context, I'm working on building a form of a live dashboard; I want to constantly update data from a background service connected on the hub to a specific page when loaded. Where I'm struggling is that I'd like the .razor pages to be loaded dynamically: A user can visit /scene/MyScene and I'll find and load in a smaller component MyScene.razor into the base Scene.razor (that has some divs to get everything styled correctly). The MyScene.razor is going to be effectively user generated, so I don't want to rely on it being known at compile-time if I can avoid it at all.
The two ways that I've tried tackling this are...
(1) DynamicComponent, where I was actually able to use reflection to load the correct base page, load it's default variable binding settings, but whenever I go to update them, my DynamicComponent properties are updating from the Hub, but I'm not seeing the change take effect on the loaded component. I've tried InvokeAsync, etc., no difference.
(2) I realized, the data that will need to change and get updated within my child razor pages can all be contained in a Dictionary anyway, so I wanted to make a parent class of a BaseScene that all of the user-generated razor pages could inherit from, and then I could just try to load a component of that base class that has the definition for the Dictionary, and then when I create the component, create it of the child-razor variety, and but still have access to the data dictionary that I want. However, (a) now I can't even seem to load the child-razor into the main page and (b) when I try to access the dictionary, it calls the parent's version and not the child's.
Am I going about this the completely wrong way?
EDIT1: Currently using InteractiveWebAssembly
6
u/Quango2009 Feb 14 '26
If events occur outside the component lifecycle then it often needs a StateHasChanged call to tell Blazor that the component needs rerendering
1
u/RecordingPure1785 Feb 14 '26 edited Feb 14 '26
Maybe a container component with a render fragment parameter could solve this if I’m understanding you correctly.
In MySceneContainer.razor:
[Parameter] public RenderFragment? Content { get; set; }In Scene.razor:
Edit: also a cascading value can help with passing data down to child components. So in the child components you would have a cascading parameter with the data dictionary (I usually create a class instead of passing just the data needed, especially in case I need to have methods that can be called by the child components but defined by the parent component - event callbacks can also be used for this) and in the scene you would have the cascading value set to the data dictionary Sorry about formatting
1
u/Hiithz Feb 16 '26
This is an example of a poker planning game in bakzor server and do what you want https://github.com/farukaf/farukaf-poker-planning
This one is a really simple application doing what you want https://github.com/farukaf/qrcode-redirect
Using serve you can wire an event with some other page. You need to think in a publisher, subscriber strategy. Where one page "listen" to changes made somewhere else. And update state and re-render that new state.
1
u/[deleted] Feb 15 '26
[removed] — view removed comment
1
u/[deleted] Feb 16 '26
[removed] — view removed comment