Data Binding
Data binding keeps a C# field and an input in sync in both directions with a single attribute.
On this page · 5 min · Intermediate
On this page
- Learning objectives
- Explanation
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Intermediate
- Updated
- Oct 29, 2026
Interview questions
Learning objectives#
- Bind inputs with @bind
- Choose when binding updates with @bind:event
- Format values and run code after binding
- Create bindable component parameters
Explanation#
@bind="field" sets the input’s value from the field and updates the field when the input changes.
By default it updates on change (when the input loses focus). @bind:event="oninput" updates on every keystroke.
@bind:format formats dates; @bind:after runs a method after the value is updated — handy for search-as-you-type.
A component can support @bind-Value by exposing Value and ValueChanged (EventCallback<T>) parameters.
Code example: Live product filter#
@page "/filter"
@rendermode InteractiveServer
<input @bind="search" @bind:event="oninput" @bind:after="Apply" placeholder="Search…" />
<select @bind="maxPrice" @bind:after="Apply">
<option value="100000">Any price</option>
<option value="5000">Under ₹5,000</option>
</select>
<label><input type="checkbox" @bind="inStockOnly" @bind:after="Apply" /> In stock only</label>
<p>@results.Count result(s)</p>
@code {
private string search = "";
private int maxPrice = 100000;
private bool inStockOnly;
private List<Product> all = Product.Samples, results = Product.Samples;
private void Apply() => results = all
.Where(p => p.Name.Contains(search, StringComparison.OrdinalIgnoreCase))
.Where(p => p.Price <= maxPrice && (!inStockOnly || p.Stock > 0))
.ToList();
}
Typing “mo” immediately shows “1 result(s)” (Mouse). Changing the price or ticking “In stock only” re-filters at once.
This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.
Step-by-step explanation#
- @bind:event="oninput" updates search on every keystroke.
- @bind:after calls Apply after each value change.
- select and checkbox bindings convert to int and bool automatically.
- Apply recomputes the results list, and the count re-renders.
Real-world use case#
Admin tables with instant filters, settings forms and calculators that update totals as the user types.
Common mistakes#
@bind already uses onchange. Use @bind:after to react to changes.
Debounce, or filter on the server with paging.
Best practices#
- Use @bind:after for side effects
- Use oninput only when you need live updates
- Format dates with @bind:format
- Expose Value/ValueChanged for reusable inputs
Remember this#
@bind = value in + changes out.
Default updates on change; oninput = every keystroke.
@bind:after = run code after the update.
Practice exercise#
Your task: Bind a DateOnly field to a date input showing yyyy-MM-dd.
Show solution
<input type="date" @bind="start" @bind:format="yyyy-MM-dd" />
@code { private DateOnly start = DateOnly.FromDateTime(DateTime.Today); }
Mini quiz#
How do you make @bind update on every keystroke?
Want more? Practise all Blazor MCQs · Blazor interview questions
My notes#
Saved in this browser.