How to clear RadzenRadioButtonList in code


What is the best way to clear the items from a RadzenRadioButtonList with code?

If you create your items in for loop you can have also condition if some item(s) should be created or not. When you call StateHasChanged() of the page your items for loop will be re-executed.

Thank you for the quick reply :slight_smile:

I have two radiobutton lists in one component. The items in the second list are added with a foreach loop (from a sql db) based on what is selected in the first radiobutton list, so the items can be changed multiple times.

The function fired when the first radiobuttonlist is changed is similar to this:

 private async void RadioButtonList1Changed(MyClass myclass)
        await using (var context = new ApplicationDbContext(DbContextOptions))
            SecondRadioButtonListItems = await context.Table.Where(i => i.whatever).ToListAsync();
        foreach (var item in SecondRadioButtonListItems )
            RadioButtonList2.AddItem(new RadzenRadioButtonListItem<MyClass2>
                Value = item ,
                Text = item .Name
        await InvokeAsync(StateHasChanged);

Every time this runs, it adds the items to the existing radiobuttonlist. So i am really looking for å way to clear the items, before adding them again.

I was referring the .razor part of the page. It’s easier to add desired items using for loop inside Items tag.

Thanks! That works :slight_smile: