Recursive AI

Select an HTML radio button programmatically in C#

OneRender is a free, open-source HTML and CSS rendering library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

Use SetChecked to select a radio button without a click on a page that OneRender renders itself, with no browser. The previous choice in the group clears.

Terminal
dotnet add package RecursiveAI.OneRender.Image

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using OneRender;
using OneRender.Image;
using OneRender.Interaction;
 
const string html = """
    <input type=radio name=plan id=basic
           checked>
    <input type=radio name=plan id=plus>
    """;
var options = new HtmlPageOptions(
    FontProviders.CreateDefault());
var page = InteractivePage
    .Create(html, options);
var document = page.Page.Document;
var basic = document
    .GetElementById("basic");
var plus = document
    .GetElementById("plus");
page.SetChecked(plus, true);
Console.WriteLine(
    $"Basic: {page.IsChecked(basic)}");
Console.WriteLine(
    $"Plus: {page.IsChecked(plus)}");

The example is the full program below: unzip it and dotnet run. To run only the code above, paste it into Program.cs of a new dotnet new console project and dotnet run.

The new selection prints as:

Text
Basic: False
Plus: True

The full program

C#
using OneRender;
using OneRender.Image;
using OneRender.Interaction;
 
const string html = """
    <input type=radio name=plan id=basic checked>
    <input type=radio name=plan id=plus>
    """;
var options = new HtmlPageOptions(FontProviders.CreateDefault());
var page = InteractivePage.Create(html, options);
var document = page.Page.Document;
var basic = document.GetElementById("basic");
var plus = document.GetElementById("plus");
int events = 0;
foreach (var radio in new[] { basic, plus })
    foreach (var type in new[] { "click", "input", "change" })
        page.Events.AddEventListener(EventTarget.Of(radio), type, _ => events++);
 
Console.WriteLine($"Basic before: {page.IsChecked(basic)}");
page.SetChecked(plus, true);
Console.WriteLine($"Basic: {page.IsChecked(basic)}");
Console.WriteLine($"Plus: {page.IsChecked(plus)}");
Console.WriteLine($"Click, input or change events: {events}");

dotnet run prints:

Text
Basic before: True
Basic: False
Plus: True
Click, input or change events: 0

The full program listens for click, input and change on both radios. The zero count is expected: SetChecked changes form state without dispatching events. Pass an <input type=radio> from the page; passing a non-checkable node returns InputOutcome.None and doesn’t change the page.

Variations

Clear the selected option. Add this after selecting plus to leave the group with no selection:

C#
page.SetChecked(plus, false);
Console.WriteLine(page.IsChecked(plus));

The short answer

Call page.SetChecked(radio, true) on the radio input in a OneRender InteractivePage. It checks that option and unchecks any previously checked option in the group. Read the live value with page.IsChecked, and use the same InteractivePage when you render or inspect the form again. You don’t need to change the source markup or send a pointer event.

How do I select a radio button in HTML from C#?

Use checked in the HTML for the initial choice. After creating the page, use SetChecked to restore a preference or select an option in response to another part of your app. The sample deliberately leaves checked on Basic in the input string. After the change, IsChecked(basic) is false: looking for that attribute in the document would still tell you what the initial markup said, not what the control shows now. OneRender also uses the live state when styling the :checked selector.

How do you group radio buttons in HTML?

Give the choices the same nonempty name and the same form owner. The example’s two inputs are both outside a form, so sharing name=plan is enough. Their IDs are different so the host can look up each one. Matching names in different forms don’t make one group. If selecting an option leaves another one checked, inspect the names and which form owns each control.

SetChecked is for changing state, not emulating a user action. If your app has a handler that normally recalculates a total in response to change, call that calculation from the code that restores the selection too; the state change alone won’t invoke the handler.

All onerender recipes