
Blazor is a modern web framework developed by Microsoft that is changing the way developers build interactive web applications. Blazor lets you build rich client-side web experiences using C# and .NET instead of JavaScript. One of the most powerful features of Blazor is its component-based architecture, which encourages the reuse of code and modular design. This article will show you how to create a reusable dynamic pagination component with Blazor, ensuring minimal changes and maximum flexibility.
To learn more about Blazor Web Development, click here.
Understanding Blazor and Its Component-Based Architecture
Blazor is part of the ASP.NET Core framework, and it allows you to develop full-stack web applications with .NET. Blazor provides a unified programming model that lets you use your .NET skills across the stack, whether you’re building server-side or client-side applications.
Blazor differs from traditional web frameworks by running .NET code in the browser (Blazor WebAssembly) or on the server with SignalR (Blazor Server). This removes the need for JavaScript in many scenarios, providing a more seamless development experience for .NET developers.
Component-Based Framework
- Blazor is a component-based framework at its core. In Blazor, everything is a component, from a simple button to a complex data grid. Components are self-contained units of user interface and behavior that can be reused throughout the entire application. They allow developers to encapsulate their HTML, CSS, and C# code in one location, which promotes reusability, maintainability, and testability.
- Components can be nested, set parameters, and even share data with each other. This makes Blazor an ideal framework for building modular and dynamic web applications. Once created, components can be used anywhere in the application and are written as .razor files.
- In this article, we create a pagination component that can be reused across the application dynamically for any list or collection.
Pagination in Web Applications
- In web applications that deal with large amounts of data, pagination is a common feature. This enables you to split your data into pages so that users can browse more easily without overwhelming them with lengthy lists. Correct pagination improves the user experience because it improves performance, reduces page loading time, and makes data easier to digest.
- You can implement both client-side and server-side pagination approaches in Blazor. In this article, our focus will be on the creation of a reusable client-side pagination component that can be easily integrated into any Blazor application.
Complete Source Code: https://github.com/rijwanansari/DynamicPagination
Creating a Reusable Dynamic Pagination Component
Let’s walk through the process of building a reusable pagination component in Blazor that supports dynamic page size selection and navigation.
Create a Blazor Web App project template using Visual Studio or VS Code and give a name.

You can choose any .NET Core framework. However, for this article, I have used .NET 8.
Defining the Pagination Component
Create a Blazor component in a shared folder or any folder of your choice where you can share across the project easily.
The first step is to define the pagination component. This component will handle the pagination logic and display the necessary controls, such as page numbers and page size options.
PaginationComponent.razor
@typeparam TItem
<div>
<table class="table">
<thead>
@ChildContentHeader
</thead>
<tbody>
@foreach (var item in PaginatedData)
{
@ChildContentRow(item)
}
</tbody>
</table>
<div class="pagination-controls">
<!-- Page Size Dropdown -->
<label for="pageSize">Page Size: </label>
<select @bind="PageSize" id="pageSize">
@foreach (var size in PageSizes)
{
<option value="@size">@size</option>
}
</select>
<!-- Previous Page Button -->
<button @onclick="PreviousPage" disabled="@IsPreviousDisabled">Previous</button>
<!-- Page Number Buttons -->
@foreach (var pageNumber in Enumerable.Range(1, TotalPages))
{
<button @onclick="() => GoToPage(pageNumber)" class="@(CurrentPage == pageNumber ? "active" : "")">
@pageNumber
</button>
}
<!-- Next Page Button -->
<button @onclick="NextPage" disabled="@IsNextDisabled">Next</button>
</div>
</div>
@code {
[Parameter] public IEnumerable<TItem> Items { get; set; }
[Parameter] public int DefaultPageSize { get; set; } = 10;
[Parameter] public RenderFragment ChildContentHeader { get; set; }
[Parameter] public RenderFragment<TItem> ChildContentRow { get; set; }
private int PageSize { get; set; }
private List<TItem> PaginatedData => Items.Skip((CurrentPage - 1) * PageSize).Take(PageSize).ToList();
private int CurrentPage { get; set; } = 1;
private readonly int[] PageSizes = new[] { 5, 10, 20, 50 };
protected override void OnInitialized()
{
PageSize = DefaultPageSize;
}
private void NextPage()
{
if (CurrentPage < TotalPages)
{
CurrentPage++;
}
}
private void PreviousPage()
{
if (CurrentPage > 1)
{
CurrentPage--;
}
}
private void GoToPage(int pageNumber)
{
CurrentPage = pageNumber;
}
private int TotalPages => (int)Math.Ceiling(Items.Count() / (double)PageSize);
private bool IsPreviousDisabled => CurrentPage == 1;
private bool IsNextDisabled => CurrentPage == TotalPages;
}


Join the conversation! Your thoughts help the community grow.