Getting Started with the RadioButton
This tutorial explains how to set up a basic Telerik UI for ASP.NET Core 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. Finally, you can run the sample code in Telerik REPL and continue exploring the components.
Prerequisites
To successfully complete the tutorial, you need a project that is already configured to use the Telerik UI for ASP.NET Core components:
You can use the Telerik REPL playground and skip installing the components on your system and configuring a project.
-
You can prepare a Visual Studio project by following the following guide:
- Creating a new pre-configured project for the Telerik UI for ASP.NET Core components from a project template.
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 Core HtmlHelpers:
@using Kendo.Mvc.UI
-
To use the Telerik UI for ASP.NET Core TagHelpers:
@addTagHelper *, Kendo.Mvc
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>
@addTagHelper *, Kendo.Mvc
<h4>RadioButton</h4>
<div>
</div>
2. Initialize the RadioButton
Use the RadioButton HtmlHelper or TagHelper to add the component to the view:
- The
Name()
configuration method is mandatory as its value is used for theid
and thename
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 theHtmlAttributes()
option to set an identicalname
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>
@addTagHelper *, Kendo.Mvc
<h4>RadioButton</h4>
<div>
<kendo-radiobutton id="role1" name="role" checked="true" label="Admin"></kendo-radiobutton>
<kendo-radiobutton id="role2" name="role" label="User"></kendo-radiobutton>
</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>
@addTagHelper *, Kendo.Mvc
<h4>RadioButton</h4>
<div>
<kendo-radiobutton id="role1"
name="role"
checked="true"
label="Admin"
on-change="onChange">
</kendo-radiobutton>
<kendo-radiobutton id="role2"
name="role"
label="User"
on-change="onChange">
</kendo-radiobutton>
</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:
-
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>
-
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>
Explore this Tutorial in REPL
You can continue experimenting with the code sample above by running it in the Telerik REPL server playground: