RadioButton

in namespace DotVVM.BusinessPack.Controls

Renders the HTML radio button.

Usage & Scenarios

Inherits the built-in RadioButton control with a DotVVM Business Pack visual style.

Sample 1: Basic RadioButtons

The RadioButton control has the Checked property of boolean which indicates whether the control is checked or not.

Optionally, you can use the Text property to specify the label for the radio button. Or you can put the label contents inside the bp:RadioButton element.

<bp:RadioButton Checked="{value: IsCompany}" 
                CheckedValue="{value: true}"
                GroupName="RadioGroup" 
                Text="company" />

<bp:RadioButton Checked="{value: IsCompany}" 
                CheckedValue="{value: false}"
                GroupName="RadioGroup">
    person <em>(default)</em>
</bp:RadioButton>

<p>Is company: {{value: IsCompany}}</p>
using DotVVM.Framework.ViewModel;

namespace DotvvmWeb.Views.Docs.Controls.businesspack.RadioButton.sample1
{
    public class ViewModel : DotvvmViewModelBase
    {
        public bool IsCompany { get; set; }
    }
}

Sample 2: Multiple RadioButtons

The RadioButton has also a property CheckedItem which point to a property in the viewmodel. This is an alternative to the Checked property - they cannot be combined.

The property referenced by the CheckedItem, holds the value of the radio button which is checked.

In the example, the second radio button will be checked when the page loads. That's because the initial value of the Color property is "g". If you click another radio button, the property value will be updated immediately

<bp:RadioButton CheckedItem="{value: Color}" CheckedValue="r" Text="R" />
<bp:RadioButton CheckedItem="{value: Color}" CheckedValue="g" Text="G" />
<bp:RadioButton CheckedItem="{value: Color}" CheckedValue="b" Text="B" />

<p>Selected color: {{value: Color}}</p>
using DotVVM.Framework.ViewModel;

namespace DotvvmWeb.Views.Docs.Controls.businesspack.RadioButton.sample2
{
    public class ViewModel : DotvvmViewModelBase
    {
        public string Color { get; set; } = "g";
    }
}

Sample 3: RadioButton's Changed Event

The RadioButton has the Changed event which is fired whenever the radio button is checked or unchecked.

Notice the CheckedItem and CheckedValue properties. The first radio button will set true to the Value property in the viewmodel, the second one will set false. he value binding in the CheckedValue must be used, otherwise the true and false values would be treated as strings.

<bp:RadioButton CheckedItem="{value: Value}" CheckedValue="{value: true}" Changed="{command: OnChanged()}" />
<bp:RadioButton CheckedItem="{value: Value}" CheckedValue="{value: false}" Changed="{command: OnChanged()}" />

<p>{{value: NumberOfChanges}}</p>
using DotVVM.Framework.ViewModel;

namespace DotvvmWeb.Views.Docs.Controls.businesspack.RadioButton.sample3
{
    public class ViewModel : DotvvmViewModelBase
    {
        public bool Value { get; set; }

        public int NumberOfChanges { get; set; } = 0;

        public void OnChanged()
        {
            NumberOfChanges++;
        }
    }
}

Sample 4: Real World Sample

Final price is calculated by adding a tax to price of selected beverage. Prices are set in CheckedValues property and the total price is updated after any change by Changed event.

The Business Pack includes the RadioButtonList, which would make this sample much simpler. If you need to generate multiple radio buttons from a collection in a viewmodel, you can use this control.

<h3>Fruits</h3>
<p>
    <bp:RadioButton CheckedItem="{value: PriceWithoutTax}" 
                    Changed="{command: UpdatePrice()}"
                    CheckedValue="{value: 1}" 
                    Text="Apple" />

    <bp:RadioButton CheckedItem="{value: PriceWithoutTax}" 
                    Changed="{command: UpdatePrice()}"
                    CheckedValue="{value: 3}" 
                    Text="Lemon" />

    <bp:RadioButton CheckedItem="{value: PriceWithoutTax}" 
                    Changed="{command: UpdatePrice()}"
                    CheckedValue="{value: 6}" 
                    Text="Orange" />

    <bp:RadioButton CheckedItem="{value: PriceWithoutTax}" 
                    Changed="{command: UpdatePrice()}"
                    CheckedValue="{value: 5}" 
                    Text="Pinapple" />
</p>

<h3>Tax</h3>
<p>
    <bp:RadioButton CheckedItem="{value: Tax}" 
                    Changed="{command: UpdatePrice()}"
                    CheckedValue="{value: 5}" 
                    Text="Reasonable" />

    <bp:RadioButton CheckedItem="{value: Tax}" 
                    Changed="{command: UpdatePrice()}"
                    CheckedValue="{value: 10}" 
                    Text="Still quite reasonable" />

    <bp:RadioButton CheckedItem="{value: Tax}" 
                    Changed="{command: UpdatePrice()}"
                    CheckedValue="{value: 20}" 
                    Text="European" />

    <bp:RadioButton CheckedItem="{value: Tax}" 
                    Changed="{command: UpdatePrice()}"
                    CheckedValue="{value: 40}" 
                    Text="European in 10 years" />
</p>

<p>Total price: ${{value: Price}}</p>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DotVVM.Framework.ViewModel;

namespace DotvvmWeb.Views.Docs.Controls.businesspack.RadioButton.sample4
{
	public class ViewModel : DotVVM.BusinessPack.DocSamples.Samples.LayoutViewModel
	{
	    public float PriceWithoutTax { get; set; }

	    public float Tax { get; set; }

	    public float Price { get; set; }

	    public void UpdatePrice()
	    {
	        Price = PriceWithoutTax + ((PriceWithoutTax / 100) * Tax);
	    }

    }
}

Properties

Name Type Description Notes Default Value
property icon Attributes Dictionary<String,Object>
attribute
inner element
static value
bindable
default
null
property icon AutoFocus Boolean Gets or sets whether the control should have focus when page loads or when a dialog is opened. The default value is false.
attribute
inner element
static value
bindable
default
False
property icon Checked Boolean Gets or sets whether the control is checked.
attribute
inner element
static value
bindable
default
False
property icon CheckedItem Object Gets or sets the CheckedValue of the first RadioButton that is checked and bound to this collection.
attribute
inner element
static value
bindable
default
null
property icon CheckedValue Object Gets or sets the value that will be used as a result when the control is checked. Use this property in combination with the CheckedItem or CheckedItems property.
attribute
inner element
static value
bindable
default
null
property icon ClientIDMode ClientIDMode Gets or sets the client ID generation algorithm.
attribute
inner element
static value
bindable
default
Static
property icon DataContext Object Gets or sets a data context for the control and its children. All value and command bindings are evaluated in context of this value.
attribute
inner element
static value
bindable
default
null
property icon Enabled Boolean Gets or sets a value indicating whether the control is enabled and can be clicked on.
attribute
inner element
static value
bindable
default
True
property icon GroupName String Gets or sets an unique name of the radio button group.
attribute
inner element
static value
bindable
default
property icon ID String Gets or sets the unique control ID.
attribute
inner element
static value
bindable
default
null
property icon InnerText String Gets or sets the inner text of the HTML element.
attribute
inner element
static value
bindable
default
null
property icon RenderEmptyLabel Boolean Gets or sets whether to render an empty <label> element when the Text is empty. It allows to use custom RadioButton themes. The default value is true.
attribute
inner element
static value
bindable
default
True
property icon TabIndex Int32 Gets or sets the order in which the control is reachable in sequential keyboard navigation. The default value is 0 which means the document order.
attribute
inner element
static value
bindable
default
0
property icon Text String Gets or sets the label text that is rendered next to the control.
attribute
inner element
static value
bindable
default
property icon Visible Boolean Gets or sets whether the control is visible.
attribute
inner element
static value
bindable
default
True

Events

Name Type Description
event icon Changed Command Gets or sets the command that will be triggered when the control check state is changed.

HTML produced by the control

With no Text or an inner content specified, the control renders just the radio button.

<input type="radio" data-bind="..." />

If there is a Text or an inner content, the label is rendered around the radio button.

<label>
  <input type="radio" data-bind="..." />
  Text or inner content
</label>