---
title: "Focus an input after a C# component updates"
url: https://recursiveai.org/oneui/cookbook/focus-input-after-component-update/
description: "Set keyboard focus when a OneUI update adds a search input. Bind an ElementRef in HTML and call Focus from the C# component’s OnMounted callback."
version: "1.0.0"
---

# Focus an input after a C# component updates

**OneUI** is a free, open-source .NET library from Recursive AI for writing UI components in HTML, CSS, and C# that run in desktop windows or in a browser via WebAssembly with OneApp. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneUI) · [GitHub](https://github.com/recursive-ai-oss/oneui-csharp) · [Docs](https://recursiveai.org/oneui/docs/) · [Download example (.zip)](https://recursiveai.org/oneui/cookbook/focus-input-after-component-update/focus-input-after-component-update.zip)

Bind `ref` in `SearchBox.html` and call `Focus()` in `OnMounted`.

```shell
dotnet add package RecursiveAI.OneUI
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

`SearchBox.html`:

```html
<input id="search" aria-label="Search"
       ref="{Search}" />
```

[The example](https://recursiveai.org/oneui/cookbook/focus-input-after-component-update/focus-input-after-component-update.zip) is the full program below: unzip it and `dotnet run`.

OneUI generates the code that binds the reference.

## The full program

Pair `SearchBox.html` with `SearchBox.cs` and `SearchPanel.html` with `SearchPanel.cs`: each template and partial class share a name and folder. Clicking Search sets `IsOpen`, so OneUI mounts `SearchBox` and focuses its input. The console sample does this in a `ReferenceDocument` without opening a window.

`SearchBox.cs`:

```csharp
using OneUI.Components;

public partial class SearchBox
{
    ElementRef Search { get; } = new();

    protected override void OnMounted()
        => Search.Focus();
}
```

`SearchPanel.html`:

```html
<button id="open" on:click="Open">Search</button>
<template if="{IsOpen}">
  <SearchBox />
</template>
```

`SearchPanel.cs`:

```csharp
using OneUI.Components;

public partial class SearchPanel
{
    [State]
    public partial bool IsOpen { get; set; }

    void Open() => IsOpen = true;
}
```

`Program.cs`:

```csharp
using OneUI.Components;
using OneUI.Components.Reference;

var doc = new ReferenceDocument();
var ui = new UiContext(doc);
doc.Context = ui;
ui.Mount(new SearchPanel());

Console.WriteLine($"Before: {FocusedId()}");
doc.Click("#open");
Console.WriteLine($"After: {FocusedId()}");

string FocusedId() => doc.ActiveElement?.GetAttribute("id") ?? "none";
```

`dotnet run` prints:

```text
Before: none
After: search
```

Calling `Focus()` before OneUI binds the reference throws `InvalidOperationException`. A bound input that’s disabled or hidden won’t take focus.

## Variations

For focus as soon as the panel opens, replace the state declaration in `SearchPanel.cs` with the code below. The search box now mounts with its parent and the `Before` line already reports `search`.

```csharp
[State]
public partial bool IsOpen { get; set; }

public SearchPanel() => IsOpen = true;
```

## Related recipes

- [Build a UI component without HTML templates](https://recursiveai.org/oneui/cookbook/build-ui-component-without-markup/)
- [Pass HTML into named component slots](https://recursiveai.org/oneui/cookbook/pass-html-content-to-component-slots/)

## The short answer

Put `ref="{Search}"` on the input and declare an `ElementRef` named `Search` on its partial class. Call `Search.Focus()` in `OnMounted` to focus a newly added input. If a button reveals the input later, put the input in a child component and mount that child conditionally. Its callback then runs when it appears, not when the whole screen first opened.

## How do you focus an input on a button click after it’s added?

When the button handler sets `IsOpen`, OneUI doesn’t immediately insert the input in the middle of the handler. Event dispatch batches state changes, and the document applies the resulting changes together. Trying to focus the not-yet-created input immediately after assigning the flag can fail. Use the new child’s `OnMounted` callback, which runs after the commit that inserts its nodes. `OnMounted` isn’t a callback for every update: a parent that’s already mounted doesn’t get another call when its child appears.

## Why doesn’t the input get keyboard focus?

Distinguish an unbound reference from a bound reference that can’t take focus. An unbound reference throws: check that the `ref` is on the input itself and that the conditional branch currently creates it. A focus request on a disabled, hidden or detached element has no effect. If the input already exists and is focusable, you can call `Focus()` in a handler without remounting the component. Use `ActiveElement` in the reference document to see which element received focus. See [the focus lifecycle](https://recursiveai.org/oneui/docs/markup/#focus) for what counts as focusable and which events the document sends back.
