DataBinder Utility
The DataBinder class provides backward-compatible Eval() methods
that emulate the Web Forms data binding syntax. It is marked [Obsolete] and
should be migrated away from — see the Moving On section below.
1. DataBinder.Eval with a Repeater
Use DataBinder.Eval(Container.DataItem, "PropertyName") to bind data
inside a Repeater template, just like Web Forms.
| ID | Name | Price |
|---|
Code:
@using BlazorWebFormsComponents
<Repeater Context="Item" ItemType="Widget">
<ItemTemplate>
<tr>
<td>@DataBinder.Eval(Item, "Id")</td>
<td>@DataBinder.Eval(Item, "Name")</td>
<td>@DataBinder.Eval(Item, "Price")</td>
</tr>
</ItemTemplate>
</Repeater>
2. Shorthand Eval with Static Import
Add @using static BlazorWebFormsComponents.DataBinder to use
the shorter Eval("PropertyName") syntax — closer to the Web Forms
<%# Eval("PropertyName") %> pattern.
| ID | Name | Price |
|---|
Code:
@using static BlazorWebFormsComponents.DataBinder
<Repeater Context="Item" ItemType="Widget">
<ItemTemplate>
<tr>
<td>@Eval("Id")</td>
<td>@Eval("Name")</td>
<td>@Eval("Price")</td>
</tr>
</ItemTemplate>
</Repeater>
3. Eval with Format Strings
Use Eval("PropertyName", "{0:C}") to apply .NET format strings,
just like <%# Eval("Price", "{0:C}") %> in Web Forms.
| Name | Price (formatted) |
|---|
Code:
<Repeater Context="Item" ItemType="Widget">
<ItemTemplate>
<tr>
<td>@Eval("Name")</td>
<td>@Eval("Price", "{0:C}")</td>
</tr>
</ItemTemplate>
</Repeater>
4. Moving On — Use @context Directly
DataBinder.Eval is marked [Obsolete]. In Blazor, you have
direct access to the strongly-typed @context (or your named Context
variable) inside templates. This is simpler, faster, and gives you compile-time checking.
| ID | Name | Price |
|---|
Code:
@ No DataBinder needed — just use the context variable directly!
<Repeater Context="Item" ItemType="Widget">
<ItemTemplate>
<tr>
<td>@Item.Id</td>
<td>@Item.Name</td>
<td>@Item.Price.ToString("C")</td>
</tr>
</ItemTemplate>
</Repeater>
Why migrate? The @context.Property approach gives you
IntelliSense, compile-time type safety, and eliminates the reflection overhead of
DataBinder.Eval.