Articles in this section
Category / Section

How to implement Radio Buttons in a ListBox Component with Syncfusion Blazor?

2 mins read

This article demonstrates how to integrate radio buttons within Blazor ListBox component in a Blazor application using Syncfusion Blazor components. The example provided will guide you through the process of adding SfRadioButton elements to a SfListBox to create a selectable list of options.

Prerequisites

Before proceeding, ensure you have the following:

  • .NET Core 3.1 SDK or later
  • Visual Studio 2019 or later
  • Syncfusion Blazor components installed in your project

Implementation Steps

  1. Namespace Inclusions:

    Begin by including the necessary namespaces in your Razor page:

    @using Syncfusion.Blazor.DropDowns
    @using Syncfusion.Blazor.Buttons
    
  2. ListBox Component Setup:

    Define the SfListBox component with a TValue of string[] and bind it to a data source:

    <SfListBox Value="@Selects" TValue="string[]" DataSource="@Data" TItem="ListData">
      <ListBoxFieldSettings Text="Text"></ListBoxFieldSettings>
      <ListBoxTemplates TItem="ListData">
        <ItemTemplate>
          <SfRadioButton @onchange="onchange" Label="@((context as ListData).Text)" Name="listbox" Value="@((context as ListData).Text)" Checked="@checkedValue"></SfRadioButton>
        </ItemTemplate>
      </ListBoxTemplates>
    </SfListBox>
    
    <span><b>Selected Item: </b></span>
     @foreach (var item in Selects)
     {
         <span>@item</span>
     }
    
  3. RadioButton Configuration:

    Within the ItemTemplate, configure the SfRadioButton to display the text and manage the checked state:

    <SfRadioButton @onchange="onchange" Label="@((context as ListData).Text)" Name="listbox" Value="@((context as ListData).Text)" Checked="@checkedValue"></SfRadioButton>
    
  4. Event and Data Source Configuration:

    Configure the data source for the ListBox by creating a list of items with the desired text:

    @code {
        private string checkedValue = "Javascript";
        public ListData Model = new ListData();
        private string[] Selects { get; set; } = new string[1];
        
        public void onchange(Microsoft.AspNetCore.Components.ChangeEventArgs args)
        {
            Selects[0] = args.Value.ToString();
            StateHasChanged();
        }
    
        public List<ListData> Data = new List<ListData>
        {
            new ListData { Text = "Javascript"},
            new ListData { Text = "Typescript"},
            new ListData { Text = "Angular" },
            new ListData { Text = "React"},
            new ListData { Text = "Vue"}
        };
    }
    
  5. Data Model Definition:

    Define the ListData class to represent the items in the ListBox:

    public class ListData
    {
      public string Text { get; set; }
    }
    

Demo

To see a live example of the implementation, visit the following Example: Listbox with Radiobutton

Additional References

Conclusion

I hope you enjoyed learning about how to implement Radio Buttons in a ListBox Component with Syncfusion Blazor.

You can refer to our Blazor ListBox feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our Blazor ListBox example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, support portal, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied