---
title: "ASP.NET Core Blazor Component Virtualization in .NET 5 – An Overview"
published_at: "2021-05-18T10:26:34+00:00"
modified_at: "2026-02-10T11:23:36+00:00"
url: "https://www.syncfusion.com/blogs/post/asp-net-core-blazor-component-virtualization-in-net-5"
excerpt: "Virtualization is a technique that helps you to process and render only the items that are currently visible on the page (in the content viewport). We can use this technique when dealing with large amounts of data, where processing all..."
taxonomy_category:
  - "Blazor"
  - "DataGrid"
  - "Web"
taxonomy_post_tag:
  - "ASP.NET Core"
  - "Blazor"
  - "Virtual scrolling"
  - "Web Development"
  - "WebAssembly"
---

# ASP.NET Core Blazor Component Virtualization in .NET 5 – An Overview

[Mohamed Yasir Kader Sha](https://www.syncfusion.com/blogs/author/mohamedyasirk)

![ASP.NET Core Blazor Component Virtualization in .NET 5 – An Overview](https://www.syncfusion.com/blogs/wp-content/uploads/2021/05/ASP.NET-Core-Blazor-Component-Virtualization-in-.NET-5-%E2%80%93-An-Overview.png)


Virtualization is a technique that helps you to process and render only the items that are currently visible on the page (in the content viewport). We can use this technique when dealing with large amounts of data, where processing all the data and displaying the result will take time.

For example, if we want to display thousands of records in a table where the viewport can only hold 20 rows of data, we can use virtualization and process only the necessary data. Other data will be loaded and displayed dynamically when scrolling up or down.

Following are the prerequisites for implementing component virtualization:

- Visual Studio 2019 (update 16.8 or later)
- .NET 5 or later ([Download link of .NET 5](https://dotnet.microsoft.com/download/dotnet/5.0) )

## Creating a Blazor WebAssembly application with .NET 5

Virtualization in a component can be implemented either in Blazor WebAssembly or Blazor Server applications. In this blog, we are going to implement virtualization in Blazor WebAssembly.

Follow these steps to create a Blazor WebAssembly application:

1. Open Visual Studio 2019 and choose **Create a new project**.![Choose Create a new project option from the list](https://www.syncfusion.com/blogs/wp-content/uploads/2021/05/Choose-Create-a-new-project-from-the-list.png)
2. Type **Blazor** in the search window, select ** Blazor WebAssembly App**from the list and click** Next**. ![Select Blazor WebAssembly App from the list and click Next](https://www.syncfusion.com/blogs/wp-content/uploads/2021/05/Select-Blazor-WebAssembly-App-from-the-list-and-click-Next.png)
3. Enter your project name and click **Next**. Here, I am choosing the name ** BlazorVirtualizationSample**. ![Enter your project name and click Next](https://www.syncfusion.com/blogs/wp-content/uploads/2021/05/Enter-your-project-name-and-click-Next.png)
4. Then, the Additional information dialog will open. In that, select .NET 5.0 as the target framework and click **Create**. ![Select .NET 5.0 as the target framework and click Create](https://www.syncfusion.com/blogs/wp-content/uploads/2021/05/Select-.NET-5.0-as-the-target-framework-and-click-Create.png)

The Blazor WebAssembly application will be created, as shown in the following screenshot.  
 ![Created Blazor WebAssembly project](https://www.syncfusion.com/blogs/wp-content/uploads/2021/05/Created-Blazor-WebAssembly-project.png)

## Implementing virtualization

By default, the following three Razor pages will be included in the Blazor WebAssembly project:

- Counter.razor
- FetchData.razor
- Index.razor

Now we are going to create a separate Razor page called **Employee.razor** and implement virtualization.

**Employee.razor**

```
@page "/employee"

<h2>Employee Details</h2>

@if (employees == null)
{
    <p><em>Loading Employees...</em></p>
}
else
{
    <table class="table">
        <thead>
            <tr>
                <th>Employee Id</th>
                <th>Employee Name</th>
                <th>Role</th>
            </tr>
        </thead>
        <tbody>
            @*@foreach (var employee in employees)
                {
                    <tr>
                        <td>@employee.EmployeeId</td>
                        <td>@employee.Name</td>
                        <td>@employee.Role</td>
                    </tr>
                }*@

            <Virtualize Items="employees" Context="employee">
                <tr>
                    <td>@employee.EmployeeId</td>
                    <td>@employee.Name</td>
                    <td>@employee.Role</td>
                </tr>
            </Virtualize>
        </tbody>
    </table>
}

@code {
    private List<EmployeeDetails> employees;

    protected override async Task OnInitializedAsync()
    {
        employees = await GetEmployeeDetails();
    }

    private async Task<List<EmployeeDetails>> GetEmployeeDetails()
    {
        List<EmployeeDetails> employeeList = new List<EmployeeDetails>();
        List<string> roleList = new List<string>() { ".Net Developer", "Testing Engineer", "Graphic Designer", "Technical Writer", "Support Coordinator" };
        int roleArrayIndex = 0;
        int tempCount = 0;
        int employeeDataCount = 10000;

        for (int i = 1; i <= employeeDataCount; i++)
        {
            if (i > tempCount + employeeDataCount / roleList.Count())
            {
                roleArrayIndex++;
                tempCount = i - 1;
            }

            var employeeDetails = new EmployeeDetails()
            {
                EmployeeId = i,
                Name = "Employee " + i.ToString(),
                Role = roleList[roleArrayIndex]
            };

            employeeList.Add(employeeDetails);
        }
        return await Task.FromResult(employeeList);
    }

    public class EmployeeDetails
    {
        public int EmployeeId { get; set; }

        public string Name { get; set; }

        public string Role { get; set; }
    }

}
```

In **Employee.razor**, the GetEmployeeDetails() method returns 10,000 employee records with ** Employee ID**, ** Name**, and ** Role**. Following are the few differences between the ** foreach** section and Virtualize component.

In the classic approach of loading all the data during initial load, the application would take three to five seconds to load the page. Refer to the commented section of the code example.

```
@foreach (var employee in employees)
    {
        <tr>
            <td>@employee.EmployeeId</td>
            <td>@employee.Name</td>
            <td>@employee.Role</td>
        </tr>
    }
```

In the modern approach, when using virtualization, the application takes less than a second to display the initial data. Only the data that fits in the viewport is processed, so the time consumption is less than the classical approach.

```
<Virtualize Items="employees" Context="employee">
    <tr>
        <td>@employee.EmployeeId</td>
        <td>@employee.Name</td>
        <td>@employee.Role</td>
    </tr>
</Virtualize>
```

You can see in the following screenshot that only 23 rows of data were added to the table.  
 ![Blazor DataGrid with virtualization](https://www.syncfusion.com/blogs/wp-content/uploads/2021/05/Blazor-DataGrid-showing-only-limited-rows.png)

However, on scrolling down, more data will be fetched and displayed on the table.

## ItemsProvider

To take control of the number of items loaded in the table per request, use the ItemsProvider property of the Virtualize component. The ItemsProvider (delegate) asynchronously retrieves the requested items on demand (when scrolling down or up).

Add this method to @codeblock in the **Employee.razor** file.

```
private async ValueTask<ItemsProviderResult<EmployeeDetails>> LoadEmployeeDetails(ItemsProviderRequest request)
{
    var employees = await GetEmployeeDetails();
    return new ItemsProviderResult<EmployeeDetails>(employees.Skip(request.StartIndex).Take(request.Count), employees.Count());
}
```

When using ItemsProvider, no data will be loaded during the initialization time. But the Virtualize component will call the LoadEmployeeDetails method with the start index and request count (number of items to load) parameter on demand.

Also, the Virtualize component allows us to show a loading message using a place holder while data is loading.

```
<Virtualize ItemsProvider="LoadEmployeeDetails" Context="employee">
    <ItemContent>
        <tr>
            <td>@employee.EmployeeId</td>
            <td>@employee.Name</td>
            <td>@employee.Role</td>
        </tr>
    </ItemContent>
    <Placeholder>
        <p>Loading employee details...</p>
    </Placeholder>
</Virtualize>
```

Remove the following in the Employee.razor file. Instead, use a placeholder to display a loading message until the item data is available.

```
@if (employees == null)
{
    <p><em>Loading employee details...</em></p>
}
```

## ItemSize

ItemSize is the size of an element in pixels and its default value is 50 pixels.

By default, the Virtualize component measures the rendering size (height) of individual items after the initial render occurs. Use ItemSize to provide an exact item size in advance to customize the size of each row and to ensure the correct scroll position for page reloads.

```
<Virtualize Items="employees" Context="employee" ItemSize="49">
    <tr>
        <td>@employee.EmployeeId</td>
        <td>@employee.Name</td>
        <td>@employee.Role</td>
    </tr> 
</Virtualize>
```

## OverscanCount

OverscanCount determines the number of additional items rendered before and after the visible region as buffers. The default value of OverscanCount is three.

In the previous example, in the Virtualize implementation, we could see 23 data items on the page when we didn’t use the OverscanCount. If you want to load additional data items, customize the value of the OverscanCount. For example, if we set OverscanCount to 4, 24 data items load on the page.

**Note:** The default data item load count works based on the height of the container and the size of the rendered items. The data item load count might vary in different resolutions and zoom levels.

```
<Virtualize Items="employees" Context="employee" OverscanCount="4">
    <tr>
        <td>@employee.EmployeeId</td>
        <td>@employee.Name</td>
        <td>@employee.Role</td>
    </tr> 
</Virtualize>
```

## Resources

For more information, refer to the [Virtualize component in the Blazor demo](https://github.com/SyncfusionExamples/ASP.NET-Core-Blazor-Component-Virtualization-in-.NET-5)
.

## Conclusion

Thank you for reading this article! In this article, we have clearly explained how to achieve virtualization in an ASP.NET Core Blazor WebAssembly application, using the Virtualize component available in .NET 5. We also have explained customizing the virtualization. I hope you found this article informative.

Essential Studio® for [Blazor](https://www.syncfusion.com/blazor-components)
 offers the largest collection of components for the Blazor platform. It has popular components like [Charts](https://www.syncfusion.com/blazor-components/blazor-charts)
, [DataGrid](https://www.syncfusion.com/blazor-components/blazor-datagrid)
, [Scheduler](https://www.syncfusion.com/blazor-components/blazor-scheduler)
, [Diagram](https://www.syncfusion.com/blazor-components/blazor-diagram)
, [Word Processor](https://www.syncfusion.com/blazor-components/blazor-word-processor)
, and [Maps](https://www.syncfusion.com/blazor-components/blazor-map)
. It also includes unique file-format libraries for manipulating [Excel](https://www.syncfusion.com/excel-framework/blazor)
, [Word](https://www.syncfusion.com/word-framework/blazor)
, [PDF](https://www.syncfusion.com/pdf-framework/blazor)
, and [PowerPoint](https://www.syncfusion.com/powerpoint-framework/blazor)
 files. Use them to build world-class applications!

Try our Blazor components by downloading a [free 30-day trial](https://www.syncfusion.com/account/manage-trials/downloads)
, or check out our [NuGet package](https://www.nuget.org/packages/Syncfusion.Blazor)
. Feel free to peruse our [online examples](https://blazor.syncfusion.com/)
 and [documentation](https://blazor.syncfusion.com/documentation/introduction/)
 to explore other available features.

If you have questions, you can contact us through our [feedback portal](https://www.syncfusion.com/feedback/blazor-components)
, [support forums](https://www.syncfusion.com/forums/blazor-components)
, or [Direct-Trac](https://www.syncfusion.com/support/directtrac/incidents/newincident)
. We are always happy to assist you!

  
 Reference: [ASP.NET Core Blazor component virtualization](https://docs.microsoft.com/en-us/aspnet/core/blazor/components/virtualization?view=aspnetcore-5.0)
## Related blogs

- [Blazor WebAssembly: An Overview](https://www.syncfusion.com/blogs/post/blazor-webassembly-an-overview.aspx)
- [Lazy Loading Syncfusion Blazor Assemblies in a Blazor WebAssembly Application](https://www.syncfusion.com/blogs/post/lazy-loading-syncfusion-blazor-assemblies-in-a-blazor-webassembly-application.aspx)
- [How to Authenticate a Blazor WebAssembly-Hosted App with Azure Active Directory](https://www.syncfusion.com/blogs/post/how-to-authenticate-a-blazor-webassembly-hosted-app-with-azure-active-directory.aspx)
- [How to Deploy a Blazor Application in Azure App Service](https://www.syncfusion.com/blogs/post/deploy-blazor-application-in-azure-app-service.aspx)
