New to Telerik UI for ASP.NET MVC? Download free 30-day trial

Getting Started with the RadioButton

This tutorial explains how to set up a basic Telerik UI for ASP.NET MVC RadioButton and highlights the major steps in the configuration of the component.

You will initialize two RadioButton components and set one of them as checked by default.

Sample Telerik UI for ASP.NET MVC RadioButton

Prerequisites

To successfully complete the tutorial, you need a project that is already configured to use the Telerik UI for ASP.NET MVC components:

1. Prepare the CSHTML File

The first step is to add the required directives at the top of the .cshtml document:

  • To use the Telerik UI for ASP.NET MVC HtmlHelpers:

    @using Kendo.Mvc.UI
    

Optionally, you can structure the document by adding the desired HTML elements like headings, divs, paragraphs, and others.

    @using Kendo.Mvc.UI

    <h4>RadioButton</h4>
    <div>

    </div>

2. Initialize the RadioButton

Use the RadioButton HtmlHelper to add the component to the view:

  • The Name() configuration method is mandatory as its value is used for the id and the name attributes of the RadioButton element.
  • The HtmlAttributes() configuration can be used to set the values of various RadioButton Html attributes. In this tutorial, we use the HtmlAttributes() option to set an identical name attribute value to the two RadioButton components.
  • The Checked() configuration is used to set the checked/unchecked state of the RadioButton.
@using Kendo.Mvc.UI

<h4>RadioButton</h4>
<div>
    @(Html.Kendo().RadioButton()
        .Name("role1")
        .Checked(true)
        .HtmlAttributes(new { @name = "role" })
        .Label("Admin")
    )

    @(Html.Kendo().RadioButton()
        .Name("role2")
        .Checked(false)
        .HtmlAttributes(new { @name = "role" })
        .Label("User")
    )
</div>

3. Handle a RadioButton Event

The RadioButton exposes a Change event. In this tutorial, you will handle the Change event to detect any changes in the state of the component.

@using Kendo.Mvc.UI

<h4>RadioButton</h4>
<div>
    @(Html.Kendo().RadioButton()
        .Name("role1")
        .Checked(true)
        .HtmlAttributes(new { @name = "role" })
        .Label("Admin")
        .Events(ev => ev.Change("onChange"))
    )

    @(Html.Kendo().RadioButton()
        .Name("role2")
        .Checked(false)
        .HtmlAttributes(new { @name = "role" })
        .Events(ev => ev.Change("onChange"))
        .Label("User")
    )
</div>

<script>
    function onChange(e) {
        console.log(e.checked);
    }
</script>

4. (Optional) Reference Existing RadioButton Instances

You can reference the RadioButton instances that you have created and build on top of their existing configuration:

  1. Use the id attribute of the component instance to establish a reference.

    <script>
        var radiobuttonReference = $("#role2").data("kendoRadioButton"); // radiobuttonReference is a reference to the existing RadioButton instance of the helper.
    </script>
    
  2. Use the RadioButton client-side API to control the behavior of the widget. In this example, you will use the check method to check the RadioButton.

    <script>
        $(document).ready(function() {
            //get a reference to the RadioButton instance
             var radiobuttonReference = $("#role2").data("kendoRadioButton");
            //call the "check()" method
            radiobuttonReference.check(true);
        })
    </script>
    

Next Steps

See Also

In this article