Skip to content

docs(ComboBox): Revamp Rebind example #3168

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 12, 2025
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 37 additions & 42 deletions components/combobox/refresh-data.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,67 +24,62 @@ In this article:
To refresh the ComboBox data when using [`OnRead`](slug:components/combobox/events#onread), call the `Rebind` method of the TelerikComboBox reference. This will fire the `OnRead` event and execute the business logic in the handler.

````RAZOR
@* Clicking on the Rebind button will delete the first option from the dropdown and refresh the data *@

@using Telerik.DataSource.Extensions

<TelerikButton OnClick="@RebindComboBox">Rebind</TelerikButton>
<TelerikButton OnClick="@RebindComboBox">Remove First Data Item and Rebind</TelerikButton>

<TelerikComboBox @ref="@ComboBoxRef"
TItem="Product" TValue="int"
OnRead="@ReadItems"
ValueField="@nameof(Product.ProductId)"
TextField="@nameof(Product.ProductName)"
Filterable="true"
Placeholder="Find what you seek by typing"
@bind-Value="@SelectedValue">
@bind-Value="@ComboBoxValue"
OnRead="@OnComboBoxRead"
TItem="ListItem"
TValue="int"
ValueField="@nameof(ListItem.Id)"
TextField="@nameof(ListItem.Text)"
Width="200px">
</TelerikComboBox>

@code{
public int SelectedValue { get; set; }
List<Product> AllData { get; set; } = new List<Product>();
public TelerikComboBox<Product, int> ComboBoxRef { get; set; }

async Task ReadItems(ComboBoxReadEventArgs args)
{
await Task.Delay(1000);
args.Data = AllData.ToDataSourceResult(args.Request).Data;
}
public TelerikComboBox<ListItem, int>? ComboBoxRef { get; set; }
private List<ListItem> AllData { get; set; } = new();
private int ComboBoxValue { get; set; }

protected override void OnInitialized()
private void RebindComboBox()
{
List<Product> products = new List<Product>();
for (int i = 0; i < 200; i++)
if (AllData.Count > 0)
{
products.Add(new Product()
{
ProductId = i,
ProductName = "Product" + i.ToString(),
SupplierId = i,
UnitPrice = (decimal)(i * 3.14),
UnitsInStock = (short)(i * 1),
});
AllData.RemoveAt(0);
}

AllData = products;
ComboBoxValue = AllData.FirstOrDefault()?.Id ?? default;

ComboBoxRef?.Rebind();
}

public class Product
private async Task OnComboBoxRead(ComboBoxReadEventArgs args)
{
public int ProductId { get; set; }
public string ProductName { get; set; }
public int SupplierId { get; set; }
public decimal UnitPrice { get; set; }
public short UnitsInStock { get; set; }
var result = await AllData.ToDataSourceResultAsync(args.Request);
args.Data = result.Data;
args.Total = result.Total;
}

private void RebindComboBox()
protected override void OnInitialized()
{
if (AllData.Count > 0)
for (int i = 1; i <= 5; i++)
{
AllData.RemoveAt(0);
AllData.Add(new ListItem()
{
Id = i,
Text = $"ListItem {i}"
});
}

ComboBoxRef.Rebind();
ComboBoxValue = AllData.First().Id;
}

public class ListItem
{
public int Id { get; set; }
public string Text { get; set; } = string.Empty;
}
}
````
Expand Down Expand Up @@ -232,4 +227,4 @@ To refresh the ComboBox data when using [`OnRead`](slug:components/combobox/even

* [ObservableCollection](slug:common-features-observable-data)
* [INotifyCollectionChanged Interface](https://docs.microsoft.com/en-us/dotnet/api/system.collections.specialized.inotifycollectionchanged?view=netframework-4.8)
* [Live Demos](https://demos.telerik.com/blazor-ui)
* [Live Demos](https://demos.telerik.com/blazor-ui)