Skip to content

Commit abef361

Browse files
Copilotcsharpfritz
andauthored
Document Context="Item" requirement for Web Forms compatibility (#310)
Co-authored-by: csharpfritz <78577+csharpfritz@users.noreply.github.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
1 parent 147cefb commit abef361

7 files changed

Lines changed: 75 additions & 2 deletions

File tree

docs/DataControls/DataList.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ The DataList component is meant to emulate the asp:DataList control in markup an
2828
- `EnableViewState`
2929
- `ID` should be converted to `@ref` if the component is referenced in code
3030
- `ItemType` MUST be defined as an attribute
31-
- `Context` should be used to define the object used in templates. If not defined, the default `<INSERT>` will be available.
31+
- **For Web Forms compatibility, use `Context="Item"`** to access the current data item in templates with the name `@Item` instead of Blazor's default `@context`
3232

3333
## Web Forms Declarative Syntax
3434

docs/DataControls/FormView.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ The FormView component is meant to emulate the asp:FormView control in markup an
1010

1111
## Usage Notes
1212

13+
- **ItemType attribute** - Required to specify the type of items in the collection
14+
- **Context attribute** - For Web Forms compatibility, use `Context="Item"` to access the current item as `@Item` in templates (ItemTemplate, EditItemTemplate, InsertItemTemplate) instead of Blazor's default `@context`
15+
- **ID** - Use `@ref` instead of `ID` when referencing the component in code
16+
1317
## Web Forms Declarative Syntax
1418

1519
```html

docs/DataControls/GridView.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ The GridView component is meant to emulate the asp:GridView control in markup an
88
### Blazor Notes
99

1010
- The `RowCommand.CommandSource` object will be populated with the `ButtonField` object
11+
- **Context attribute** - When using `<TemplateField>`, add `Context="Item"` to access the current row item as `@Item` instead of Blazor's default `@context`
1112

1213
## Web Forms Declarative Syntax
1314

docs/DataControls/ListView.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ The ListView component is meant to emulate the asp:ListView control in markup an
2121

2222
## Usage Notes
2323

24-
- LayoutTemplate requires a `Context` attribute that defines the placeholder for the items
24+
- **LayoutTemplate** - Requires a `Context` attribute that defines the placeholder for the items
25+
- **Context attribute** - For Web Forms compatibility, use `Context="Item"` on the ListView to access the current item as `@Item` in ItemTemplate and AlternatingItemTemplate instead of Blazor's default `@context`
26+
- **ItemType attribute** - Required to specify the type of items in the collection
2527

2628
##### [Back to top](#listview)
2729

docs/DataControls/Repeater.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,29 @@ The Repeater component is meant to emulate the asp:Repeater control in markup an
3939
</SeparatorTemplate>
4040
</asp:Repeater>
4141
```
42+
43+
## Usage Notes
44+
45+
- **ItemType attribute** - Required to specify the type of items in the collection
46+
- **Context attribute** - For Web Forms compatibility, use `Context="Item"` to access the current item as `@Item` in templates instead of Blazor's default `@context`
47+
- **ID** - Use `@ref` instead of `ID` when referencing the component in code
48+
- **runat** and **EnableViewState** - Not used in Blazor (these attributes are ignored)
49+
50+
## Blazor Example
51+
52+
```razor
53+
<Repeater Items="products" ItemType="Product" Context="Item">
54+
<HeaderTemplate>
55+
<h2>Product List</h2>
56+
</HeaderTemplate>
57+
<ItemTemplate>
58+
<div class="product">@Item.Name - $@Item.Price</div>
59+
</ItemTemplate>
60+
<SeparatorTemplate>
61+
<hr />
62+
</SeparatorTemplate>
63+
<FooterTemplate>
64+
<p>End of list</p>
65+
</FooterTemplate>
66+
</Repeater>
67+
```

docs/Migration/migration_readiness.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ Throughout this document, we will refer to several framework and technology term
2020
- Class libraries referenced need to be convertible to .NET Standard
2121
- No 3rd party control libraries that don't have a shim for conversion (none available at the time of this writing)
2222

23+
### Data-Bound Controls
24+
25+
- For data-bound controls (Repeater, DataList, GridView, ListView, FormView), use `Context="Item"` attribute to access items with `@Item` instead of Blazor's default `@context`
26+
- This provides compatibility with Web Forms' strongly-typed `Item` property when using `ItemType` attribute
27+
2328
## Application architecture suggestions
2429

2530
- Prefer model-binding techniques over handling Form life-cycle events like `Init`, `Load`, `PreRender`, and `Unload`

docs/Migration/readme.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,41 @@ Alternatively, you can add the script manually to your layout:
113113

114114
For more options and details, see the [JavaScript Setup documentation](../UtilityFeatures/JavaScriptSetup.md).
115115

116+
## Step 5.6 - Data-Bound Controls and Template Context
117+
118+
When migrating data-bound controls like `<asp:Repeater>`, `<asp:DataList>`, `<asp:GridView>`, `<asp:ListView>`, and `<asp:FormView>`, you'll encounter a key difference in how templates access the current data item.
119+
120+
### Web Forms vs. Blazor Context
121+
122+
In ASP.NET Web Forms, when using strongly-typed data-binding with the `ItemType` attribute, templates access the current item using the `Item` property. In Blazor, templated components use a context variable.
123+
124+
**Blazor's Default Behavior:**
125+
By default, Blazor uses `@context` as the variable name inside templates:
126+
127+
```razor
128+
<Repeater Items="widgets" ItemType="Widget">
129+
<ItemTemplate>@context.Name</ItemTemplate>
130+
</Repeater>
131+
```
132+
133+
**Web Forms Compatibility:**
134+
For better compatibility with Web Forms conventions and to reduce migration friction, **use the `Context="Item"` attribute** on data-bound components. This allows you to use `@Item` (similar to Web Forms' `Item` property):
135+
136+
```razor
137+
<Repeater Items="widgets" ItemType="Widget" Context="Item">
138+
<ItemTemplate>@Item.Name</ItemTemplate>
139+
</Repeater>
140+
```
141+
142+
This pattern applies to all templated data controls:
143+
- `<Repeater>` - ItemTemplate, AlternatingItemTemplate, HeaderTemplate, FooterTemplate
144+
- `<DataList>` - ItemTemplate, AlternatingItemTemplate, HeaderTemplate, FooterTemplate
145+
- `<ListView>` - ItemTemplate, AlternatingItemTemplate, LayoutTemplate, GroupTemplate
146+
- `<FormView>` - ItemTemplate, EditItemTemplate, InsertItemTemplate
147+
- `<GridView>` with `<TemplateField>` - ItemTemplate
148+
149+
**Note:** While it's one extra attribute per component, using `Context="Item"` eliminates the need to learn Blazor-specific `@context` syntax and makes your templates more readable for developers familiar with Web Forms.
150+
116151
## Step 6 - Pages
117152

118153
Page migration is a process very similar to UserControls, except working on files with the `.aspx` extension.

0 commit comments

Comments
 (0)