Skip to content

Document Context="Item" requirement for Web Forms compatibility#310

Merged
csharpfritz merged 4 commits intodevfrom
copilot/default-context-to-item
Jan 30, 2026
Merged

Document Context="Item" requirement for Web Forms compatibility#310
csharpfritz merged 4 commits intodevfrom
copilot/default-context-to-item

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jan 29, 2026

Blazor's Context attribute is a Razor compiler directive, not a regular parameter. Cannot override the default "context" name at component definition level.

Research Findings

Attempted approach from issue comments:

[Parameter]
public string Context { get; set; } = "Item";

Result: Breaks Razor compilation. The compiler's special handling of Context for RenderFragment<T> is bypassed, causing "The name 'Item' does not exist in the current context" errors.

Microsoft's own components (e.g., Virtualize) use the framework default "context". This is hard-coded in Blazor's Razor compiler.

Changes

Migration Documentation

  • Migration readme: Added new Step 5.6 explaining template context differences between Web Forms' strongly-typed Item property and Blazor's @context variable
  • Migration readiness: Added "Data-Bound Controls" subsection with Context requirements

Component Documentation

Updated all five data-bound control docs with Context usage guidance:

  • Repeater: Added Usage Notes section with Context guidance and complete working example
  • DataList: Added Usage Notes with Context="Item" guidance
  • ListView: Enhanced Usage Notes with Context details
  • FormView: Added Usage Notes for all three template types (ItemTemplate, EditItemTemplate, InsertItemTemplate)
  • GridView: Enhanced Blazor Notes for TemplateField usage

Usage

Users must specify context name at component usage site:

<DataList Items="widgets" ItemType="Widget" Context="Item">
    <ItemTemplate>@Item.Name</ItemTemplate>
</DataList>

This achieves the same Web Forms-style @Item experience (matching Web Forms' strongly-typed Item property) with one attribute per component.

All documentation now provides clear, actionable guidance for developers migrating data-bound controls from Web Forms to Blazor.

Original prompt

This section details on the original issue you should resolve

<issue_title>Default the Context for a control to "Item"</issue_title>
<issue_description>Need to do some research into how Blazor sets "@context" as the default context variable name for child components. To maintain Web Forms compatibility we should reset the default for THESE COMPONENTS ONLY to @item</issue_description>

Comments on the Issue (you are @copilot in this section)

@csharpfritz Precisely! This helps the syntax adhere to the standard used in Web Forms and eliminates another piece of Blazor specific syntax that needs to be added when migrating from ASP.NET Web Forms. @csharpfritz Oooh! so we COULD override the Context parameter somewhere in the stack to force it.

Need to research a little further</comment_new>
<comment_new>@csharpfritz
so.. could we override this by adding a string Context {get;set;} to a base class and default it to Item? Hmm... feels like a good experiment.

Update: doesn't work. Tagging @rynowak and @danroth27 for insight</comment_new>
<comment_new>@csharpfritz
We’d like to extend the defaults to allow the “Item” that was the default in web forms to be usable as a default as well.

Jeff

On Feb 8, 2020, at 14:35, Ryan Nowak notifications@github.com wrote:


We deliberately do not allow a component to set the name of context. This creates a guessing game, where different components in the ecosystem have different behaviors, and users have to figure it out for each library they use.


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or unsubscribe.

@csharpfritz Unfortunately I don't think it will. Eval returns a RenderFragment that gets rendered as a string.

We would need to implement an Item property that returns the type of @context. </comment_new>


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

…lity

After extensive research and testing, I've determined that Blazor's Context
attribute is a special compile-time directive that cannot have its default
value changed from "context" to "Item" at the component definition level.

Key findings:
- Adding [Parameter] public string Context breaks Blazor's Razor compiler
- Context is NOT a regular parameter - it's a special directive
- Microsoft's own components (like Virtualize) also use default "context"
- The only solution is to document that users should use Context="Item"

Updated documentation to clarify Web Forms compatibility requirement.

Co-authored-by: csharpfritz <78577+csharpfritz@users.noreply.github.com>
Copilot AI changed the title [WIP] Set default context for control to 'Item' Document Context="Item" requirement - default override not possible Jan 29, 2026
Copilot AI requested a review from csharpfritz January 29, 2026 20:44
@csharpfritz
Copy link
Copy Markdown
Collaborator

@copilot can we introduce a default Context in the base component that sets the context to Item and allows override?

Copy link
Copy Markdown
Contributor Author

Copilot AI commented Jan 29, 2026

@copilot can we introduce a default Context in the base component that sets the context to Item and allows override?

Unfortunately, no. I tested this approach and it breaks Blazor's Razor compiler.

What I tried:
Added [Parameter] public string Context { get; set; } = "Item"; to DataBoundComponent (the base class for Repeater, DataList, ListView, FormView, etc.)

Result:

  • 207 compilation errors with "The name 'Item' does not exist in the current context"
  • Even tests that explicitly specify Context="Item" fail to compile
  • The Razor compiler's special handling of the Context attribute is bypassed

Root cause:
Context is not a regular parameter - it's a special compile-time directive processed by Blazor's Razor compiler. Adding it as a [Parameter] conflicts with this special handling, preventing the compiler from properly generating code for RenderFragment<T> templates.

Current solution:
Users must explicitly add Context="Item" at each component usage site. This is a one-line addition that provides Web Forms-style @Item syntax:

<Repeater Items="data" ItemType="Widget" Context="Item">
    <ItemTemplate>@Item.Name</ItemTemplate>
</Repeater>

This limitation exists in the Blazor framework itself - even Microsoft's components (like Virtualize) cannot override the default "context" name.

@csharpfritz csharpfritz marked this pull request as ready for review January 29, 2026 20:59
@csharpfritz
Copy link
Copy Markdown
Collaborator

@copilot Let's add notes about this context compatibility to the docs for migration

Copilot AI and others added 2 commits January 29, 2026 21:03
Added comprehensive documentation about using Context="Item" for Web Forms
compatibility across all data-bound control documentation and migration guides.

Changes:
- Migration readme: New Step 5.6 explaining template context differences
- Migration readiness: Added Context="Item" to high-level requirements
- Repeater docs: Added Usage Notes with Context guidance and example
- ListView docs: Enhanced Usage Notes with Context information
- FormView docs: Added Usage Notes with Context guidance
- GridView docs: Enhanced Blazor Notes with TemplateField context info

All documentation now clearly explains that Context="Item" should be used
for Web Forms compatibility to access items as @item instead of @context.

Co-authored-by: csharpfritz <78577+csharpfritz@users.noreply.github.com>
Addressed code review comments:
- Clarified Web Forms comparison to strongly-typed Item property
- Improved migration_readiness structure with Data-Bound Controls subsection
- Standardized ListView Usage Notes formatting for consistency

Co-authored-by: csharpfritz <78577+csharpfritz@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Jan 29, 2026

@copilot Let's add notes about this context compatibility to the docs for migration

Done! Added Context compatibility notes to migration docs in commits 86d9491 and 42ebb08.

Migration documentation:

  • New Step 5.6 explaining template context differences between Web Forms' strongly-typed Item and Blazor's @context
  • Added Data-Bound Controls subsection to migration readiness

Component documentation:

  • Added Usage Notes to Repeater, ListView, FormView with Context guidance
  • Enhanced GridView's Blazor Notes for TemplateField
  • Includes working examples showing Context="Item" usage

All data-bound controls (Repeater, DataList, ListView, FormView, GridView/TemplateField) now have clear documentation about using Context="Item" for Web Forms compatibility.

Copilot AI changed the title Document Context="Item" requirement - default override not possible Document Context="Item" requirement for Web Forms compatibility Jan 29, 2026
@csharpfritz csharpfritz merged commit abef361 into dev Jan 30, 2026
@csharpfritz csharpfritz deleted the copilot/default-context-to-item branch January 30, 2026 03:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Default the Context for a control to "Item"

2 participants