Bind a button’s label to its C# click count.
dotnet add package RecursiveAI.OneUIRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
<button on:click="Increment"> Count: {Count} </button>
Save the markup above as Counter.html in the project folder next to Counter.cs. No separate .csproj entry is needed for the template; the NuGet package finds it automatically.
The example is the full program below: unzip it and dotnet run.
The button starts at Count: 0 and shows Count: 1 after one click.
The full program
Create a dotnet new console project and add the package as shown above, or use the download with all the files in place. The NuGet package includes the generator: it matches Counter.html to Counter.cs and implements the partial properties. The example’s project file pins the package with:
<PackageReference Include="RecursiveAI.OneUI" Version="1.0.0" />Counter.cs:
using OneUI.Components; public partial class Counter { [State] public partial int Count { get; set; } private void Increment() => Count++; }
The generated [State] setter tells OneUI which bindings to update when Count changes.
Replace Program.cs with the code below. It mounts the component on a ReferenceDocument, an in-memory HTML tree, and prints the result without opening a window.
using OneUI.Components; using OneUI.Components.Reference; var dom = new ReferenceDocument(); var ui = new UiContext(dom); dom.Context = ui; ui.Mount(new Counter()); Console.WriteLine(dom.ToHtml()); dom.Click("button"); Console.WriteLine(dom.ToHtml());
dotnet run prints:
<button>Count: 0</button> <button>Count: 1</button>
If Increment is misspelled in the template, OneUI reports OUI0021 at build time. See markup diagnostics.
Variations
Count in steps of five. Replace the handler in Counter.cs:
private void Increment() => Count += 5;After one click the label reads Count: 5.
Related recipes
The short answer
Use on:click="Increment" to call a C# method and {Count} to read the current count into the button’s text. Declare Count as a [State] partial property.
What is component state in a C# user interface?
Component state is data that belongs to a component and can change what it displays. Count is an integer on the Counter instance, not text stored in the button. The click handler increments the integer. The text binding formats the latest value, so there’s no second copy of the count to keep in sync.
Use [State] for values that the component owns, such as the count or whether its details are expanded. Use [Param] when a parent supplies the value. If a value belongs to several components, keep it in their parent and pass it down; otherwise you can end up with separate counters that look like they represent the same thing.
How do you separate HTML markup from C# logic?
Keep the elements and bindings in Counter.html and the properties and handlers in the matching top-level, non-generic partial class Counter. The build matches the template to the class and generates the implementation of the partial property and the code that creates and updates the button. The on:click attribute is a OneUI directive, not an HTML inline event handler: OneUI registers a listener that calls your C# method. A handler can take no argument, as it does here, or take a MouseEvent when it needs event data.
What changes in the HTML after a click?
The reference document receives changes to its in-memory tree. In this example the button stays in place, and OneUI changes its text node. ToHtml() gives you a snapshot of the current tree, not a live HTML file. Opening a saved snapshot in a browser won’t run the C# click handler. To put the compiled component in a native window or a browser app, use a OneApp host; the console program lets you check the binding without either one.