|
| 1 | +# DataBinder |
| 2 | + |
| 3 | +In Web Forms applications, there is a somewhat standard approach of formatting and placing data in controls by using the DataBinder object. The DataBinder would be used in ItemTemplate, AlternatingItemTemplate, and other control templates to indicate where data would be formatted and placed. [Microsoft's original documentation about the DataBinder](https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.databinder?view=netframework-4.8) are available. |
| 4 | + |
| 5 | +## ASP<span></span>.NET Syntax, Support and Migration |
| 6 | + |
| 7 | +There are several common techniques that the DataBinder was used and various levels of support are provided: |
| 8 | + |
| 9 | +| Web Forms Syntax | Description | Blazor Support | |
| 10 | +| --- | --- | --- | |
| 11 | +| `DataBinder.Eval(Container.DataItem, "PropertyName")` | Output the `PropertyName` of the current item | *Fully Supported for Container.DataItem* | |
| 12 | +| `Eval("PropertyName")` | Output the `PropertyName` of the current item | *Fully Supported when using static* | |
| 13 | +| `DataBinder.Eval(Container.DataItem, "PropertyName", "FormatString")` | Output the formatted value of `PropertyName` of the current item with the `FormatString` | *Fully Supported for Container.DataItem* | |
| 14 | +| `Eval("PropertyName", "FormatString")` | Output the formatted value of `PropertyName` of the current item with the `FormatString` | *Fully Supported when using static* | |
| 15 | +| `DataBinder.GetDataItem` | Output the item currently operated on | Not supported: Replace with calls to `@context` | |
| 16 | +| `DataBinder.GetPropertyValue(Container.DataItem, "PropertyName")` | Get the property requested as an object for further handling | Only supported when passing in `@context` for the first argument. **Recommendation**: replace with `@context.PropertyName` to directly access the property in a strongly-typed manner | |
| 17 | + |
| 18 | +[Back to top](#DataBinder) |
| 19 | + |
| 20 | +## Support and Migration |
| 21 | + |
| 22 | +The DataBinder is not recommended by Microsoft in Web Forms for use in high-performance applications due to the amount of reflection used to output and format content. Similarly, we do not recommend long-term use of the DataBinder and have marked it with an `Obsolete` flag indicating that there are methods to easily migrate syntax to be more Razor-performance-friendly. |
| 23 | + |
| 24 | +[Back to top](#DataBinder) |
| 25 | + |
| 26 | +### Usage |
| 27 | + |
| 28 | +To migrate your Web Forms control that is referencing the DataBinder to Blazor, start with syntax similar to the following: |
| 29 | + |
| 30 | +```html |
| 31 | +<ItemTemplate> |
| 32 | + <li><%#: DataBinder.Eval(Container.DataItem, "Price", "{0:C}") %></li> |
| 33 | +</ItemTemplate> |
| 34 | +``` |
| 35 | + |
| 36 | +All of the databound components in this library support DataBinder syntax and can easily be converted by replacing the angle brackets with razor notation like the following: |
| 37 | + |
| 38 | +```html |
| 39 | +<ItemTemplate> |
| 40 | + <li>@DataBinder.Eval(Container.DataItem, "Price", "{0:C}")</li> |
| 41 | +</ItemTemplate> |
| 42 | +``` |
| 43 | + |
| 44 | +That's a VERY simple conversion and its clear how we can continue to deliver the same feature and formatting using Blazor. You can even shorten the syntax to use the simple `Eval` keyword and get the same effect. Just include a `@using static` keyword near the top of your Blazor page with this syntax and you can using the shortened format of `Eval`. |
| 45 | + |
| 46 | +```html |
| 47 | +@using static BlazorWebFormsComponents.DataBinder |
| 48 | +... |
| 49 | +<ItemTemplate> |
| 50 | + <li>@Eval("Price", "{0:C}")</li> |
| 51 | +</ItemTemplate> |
| 52 | +``` |
| 53 | + |
| 54 | +*Note:* Your Blazor application will emit compiler warnings while you continue to use the DataBinder. |
| 55 | + |
| 56 | +[Back to top](#DataBinder) |
| 57 | + |
| 58 | +### Moving On |
| 59 | + |
| 60 | +Moving on from the DataBinder to a more performant and simple Razor syntax is quite easy using an `ItemContext` and referencing the iterated item directly. This approach has the additional benefit of providing type-safety and compiler checking on the content of your Blazor pages. |
| 61 | + |
| 62 | +Our `Eval("Price")` statement above is further simplified into: |
| 63 | + |
| 64 | +```html |
| 65 | +<Repeater Context="Item"> |
| 66 | +... |
| 67 | + <ItemTemplate> |
| 68 | + <li>@Item.Price.ToString("C")</li> |
| 69 | + </ItemTemplate> |
| 70 | +... |
| 71 | +</Repeater> |
| 72 | +``` |
| 73 | + |
| 74 | +[Back to top](#DataBinder) |
0 commit comments