Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (150).NET Core  (24)Angular  (44)ASP.NET  (48)ASP.NET Core  (58)ASP.NET MVC  (84)Azure  (28)Blazor  (79)DocIO  (18)Essential JS 2  (67)Essential Studio  (161)Flutter  (92)JavaScript  (143)Microsoft  (101)PDF  (55)React  (32)Succinctly series  (120)Syncfusion  (460)TypeScript  (30)Uno Platform  (2)UWP  (4)Vue  (26)Webinar  (20)Windows Forms  (54)WPF  (111)Xamarin  (121)XlsIO  (24)Other CategoriesBarcode  (4)BI  (29)Bold BI  (3)Build conference  (6)Business intelligence  (53)Button  (4)C#  (105)Chart  (46)Cloud  (9)Company  (445)Dashboard  (6)Data Science  (3)Data Validation  (3)DataGrid  (38)Development  (245)Doc  (7)DockingManager  (1)eBook  (91)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (18)Extensions  (12)File Manager  (3)Gantt  (8)Gauge  (6)Git  (3)Grid  (26)HTML  (9)Installer  (2)Knockout  (2)LINQPad  (1)Linux  (1)M-Commerce  (1)Metro Studio  (11)Mobile  (195)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (21)Performance  (4)PHP  (1)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (5)Road Map  (9)Scheduler  (21)SfDataGrid  (8)Silverlight  (21)Sneak Peek  (14)Solution Services  (2)Spreadsheet  (6)SQL  (6)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (9)Tips and Tricks  (48)UI  (108)Uncategorized  (68)Unix  (2)User interface  (67)Visual State Manager  (1)Visual Studio  (18)Visual Studio Code  (10)Web  (174)What's new  (108)Windows 8  (19)Windows App  (1)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
Share on twitter
Share on facebook
Share on linkedin
How to Dynamically Render a Component in a Blazor Application

How to Dynamically Render a Component in a Blazor Application

In Blazor, we typically use either RenderTreeBuilder or Razor syntax (RenderFragment) to create dynamic components. In the classic approach, we check for the data type or for a condition to dynamically render a component. This necessitates manually maintaining the component’s state of visibility based on whether the component should be displayed or hidden, which is more complicated for complex data.

Beginning with .NET 6 Preview 1, the ASP.NET Core team introduced DynamicComponent. The use of DynamicComponent greatly reduces the complexity of dynamic component rendering when dealing with large amounts of complex data.

What is DynamicComponent?

DynamicComponent is a new built-in Blazor component that can be used to render dynamic components using its type and optional parameters. Let’s take a look at it with some code examples!

<DynamicComponent Type="@someType" />

The optional Parameters property is used to define the component properties as dictionary.

<DynamicComponent Type="@someType" Parameters="@param" />

@code {

  public Dictionary<string, object> param { get; set; }

}

Let’s see DynamicComponent in action.

Developing an expense tracker application

For this blog, we have developed an expense tracker application with dynamically created components by specifying their type.

A DropDownList contains four options for dynamically displaying the expense report. We render the expense tracker pages using several Syncfusion Blazor UI components.

Prerequisites

Refer to the Getting Started with Syncfusion Blazor Components in Blazor Server-Side App in Visual Studio 2019 documentation. It will provide a step-by-step procedure to create a Blazor server application and configure it with Syncfusion Blazor components.

The Blazor DropDownList includes object value binding support, allowing us to bind the data source object to the Value property. We obtain the name of the component class using the nameof() method and set it directly to the Type property of DynamicComponent by using the Type.GetType() method. Since we bind the data in the DropDownList, any change in the DropDownList component values will automatically update the DynamicComponent.

Refer to the following code example:

<SfDropDownList DataSource="@componentList" @bind-Value="@componentValue">
   <DropDownListFieldSettings Text="ComponentName" Value="ComponentType"></DropDownListFieldSettings>
</SfDropDownList>

<DynamicComponent Type="@(Type.GetType(componentValue.ComponentType))" Parameters="@componentValue.Param" />

@code {

    private Components componentValue { get; set; }
    public class Components
    {
        public string ComponentName { get; set; }

        public string ComponentType { get; set; }

        public Dictionary<string, object> Param { get; set; }
    }
   protected override async Task OnInitializedAsync()
    {
        this.componentList = new List<Components>()
        {
            new Components() { ComponentName = "Total Expenses", ComponentType = typeof(ExpPieChart).AssemblyQualifiedName, Param = new Dictionary<string, object>() { { "ExpenseData", ExpenseData } } }

            . . . 

            . . .
        };
        this.componentValue = componentList[0];
        await base.OnInitializedAsync();
    }
}
Dynamically rendering a component in a Blazor application
Dynamically rendering a component in a Blazor application

Resources

For more information, refer to the How to render dynamic components in Blazor application project demo.

Summary

Thank you for reading! In this article, we demonstrated how to use DynamicComponent in a Blazor server application to dynamically render new components. We also explained some details about sending parameters to the DynamicComponent.

Essential Studio for Blazor offers the largest collection of components for the Blazor platform. It has popular components like ChartsDataGridSchedulerDiagramWord Processor, and Maps. It also includes unique file-format libraries for manipulating ExcelWordPDF, and PowerPoint files. Use them to build world-class applications!

Also, try our Blazor components by downloading a free 30-day trial, or check out our NuGet package. Feel free to peruse our online examples and documentation to explore other available features.

If you have questions, you can also contact us through our feedback portalsupport forums, or Direct-Trac. We are always happy to assist you!

If you enjoyed reading this blog, then we think you shouldn’t miss the following,

Tags:

Share this post:

Share on twitter
Share on facebook
Share on linkedin

Leave a comment

Popular Now

Be the first to get updates

Subscribe RSS feed
Scroll To Top