Getting started with the Blazor TextBox component

4 Nov 20257 minutes to read

This guide explains how to include the Blazor TextBox component in a Blazor WebAssembly app using Visual Studio, Visual Studio Code, and the .NET CLI.

To get started quickly with the Blazor TextBox component, review this GitHub sample.

Prerequisites

Create a new Blazor app in Visual Studio

Create a Blazor WebAssembly App using Visual Studio via Microsoft Templates or the Syncfusion® Blazor Extension. For detailed instructions, refer to this guide.

Install Syncfusion® Blazor Inputs and Themes NuGet in the app

To add Blazor TextBox component in the app, open the NuGet package manager in Visual Studio (Tools → NuGet Package Manager → Manage NuGet Packages for Solution), search and install Syncfusion.Blazor.Inputs and Syncfusion.Blazor.Themes. Alternatively, you can utilize the following package manager command to achieve the same.

Install-Package Syncfusion.Blazor.Inputs -Version 31.2.12
Install-Package Syncfusion.Blazor.Themes -Version 31.2.12

NOTE

Syncfusion® Blazor components are available on nuget.org. Refer to the NuGet packages topic for the list of available packages and component details.

Prerequisites

Create a new Blazor app in Visual Studio Code

Create a Blazor WebAssembly App using Visual Studio Code via Microsoft Templates or the Syncfusion® Blazor Extension. For detailed instructions, refer to this guide.

Alternatively, create a WebAssembly app using the following command in the terminal (Ctrl+`).

dotnet new blazorwasm -o BlazorApp
cd BlazorApp

Install Syncfusion® Blazor Inputs and Themes NuGet in the app

  • Press Ctrl+` to open the integrated terminal in Visual Studio Code.
  • Ensure in the project root directory where the .csproj file is located.
  • Run the following commands to install the Syncfusion.Blazor.Inputs and Syncfusion.Blazor.Themes packages and restore dependencies.
dotnet add package Syncfusion.Blazor.Inputs -v 31.2.12
dotnet add package Syncfusion.Blazor.Themes -v 31.2.12
dotnet restore

NOTE

Syncfusion® Blazor components are available on nuget.org. Refer to the NuGet packages topic for the list of available packages and component details.

Prerequisites

Install the latest version of the .NET SDK. If the SDK is already installed, determine the version by running the following command in a command prompt (Windows), terminal (macOS), or shell (Linux).

dotnet --version

Create a Blazor WebAssembly app using .NET CLI

Run the dotnet new blazorwasm command to create a Blazor WebAssembly app in a command prompt (Windows), terminal (macOS), or shell (Linux).

dotnet new blazorwasm -o BlazorApp
cd BlazorApp

This command creates a new Blazor WebAssembly app in a directory named BlazorApp inside the current location. See Create Blazor app and dotnet new CLI command for more details.

Install Syncfusion® Blazor Inputs and Themes NuGet in the app

Here’s an example of how to add Blazor TextBox component in the application using the following command in the command prompt (Windows) or terminal (Linux and macOS) to install a Syncfusion.Blazor.Inputs and Syncfusion.Blazor.Themes NuGet package. See Install and manage packages using the dotnet CLI topics for more details.

dotnet add package Syncfusion.Blazor.Inputs -Version 31.2.12
dotnet add package Syncfusion.Blazor.Themes -Version 31.2.12
dotnet restore

NOTE

Syncfusion® Blazor components are available on nuget.org. Refer to the NuGet packages topic for the list of available packages and component details.

Add import namespaces

Open the ~/_Imports.razor file and import the Syncfusion.Blazor and Syncfusion.Blazor.Inputs namespaces.

@using Syncfusion.Blazor
@using Syncfusion.Blazor.Inputs

Register Syncfusion® Blazor service

Register the Syncfusion® Blazor service in the ~/Program.cs file of the Blazor WebAssembly app.

using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Syncfusion.Blazor;

var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("#app");
builder.RootComponents.Add<HeadOutlet>("head::after");

builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });

builder.Services.AddSyncfusionBlazor();
await builder.Build().RunAsync();
....

Add stylesheet and script resources

The theme stylesheet and script are available from NuGet through Static Web Assets. Include these references in the <head> section of ~/wwwroot/index.html.

<head>
    ....
    <link href="_content/Syncfusion.Blazor.Themes/bootstrap5.css" rel="stylesheet" />
    <script src="_content/Syncfusion.Blazor.Core/scripts/syncfusion-blazor.min.js" type="text/javascript"></script>

    //Blazor TextBox Component script reference.
    <!-- <script src="_content/Syncfusion.Blazor.Inputs/scripts/sf-textbox.min.js" type="text/javascript"></script> -->
</head>

NOTE

Refer to Blazor Themes to explore referencing themes via Static Web Assets, CDN, or CRG. See Adding Script Reference for script options. The Syncfusion core script loaded above covers the TextBox component; no additional component script is necessary.

Add Blazor TextBox component

Add the Syncfusion® Blazor TextBox component in the ~/Pages/Index.razor file.

<SfTextBox Placeholder='First Name'></SfTextBox>
  • Press Ctrl+F5 (Windows) or +F5 (macOS) to run the app. This renders the Syncfusion® Blazor TextBox component in the default web browser. If build issues occur, restore packages and verify the installed .NET SDK version.
Blazor TextBox component in Blazor WebAssembly

Adding icons to the Blazor TextBox

Add an icon to the TextBox component using the AddIconAsync method. The following example shows how to implement this in a Blazor WebAssembly app.

@using Syncfusion.Blazor.Inputs

<div id="sample" style="margin:130px auto;width:300px">
    <SfTextBox @ref=@TextBoxDropDownObj
               Created="@AddDateIcon"
               Placeholder="Enter Date"
               FloatLabelType="@FloatLabelType.Auto">
    </SfTextBox>
</div>


@code {
    SfTextBox TextBoxDropDownObj { get; set; }

    private async void AddDateIcon()
    {
        if (TextBoxDropDownObj != null)
        {
            //Add icon to the TextBox
            await TextBoxDropDownObj.AddIconAsync("append", "e-icons e-date-icon");
}
    }
}
Blazor TextBox with icon in Blazor WebAssembly

Floating label

The floating label lifts above the input when the TextBox is focused or contains a value. Configure it by setting the FloatLabelType API.

<SfTextBox Placeholder='First Name' FloatLabelType='@FloatLabelType.Auto'></SfTextBox>
Blazor TextBox with floating label in Blazor WebAssembly

See also