Getting Started with the ToggleButton
This tutorial explains how to set up a basic Telerik UI for ASP.NET Core ToggleButton and highlights the major steps in the configuration of the component.
You will initialize two buttons, one of them will have an event handler, an icon, and a badge. The other one will be rendered as disabled. 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 either of these guides:
Creating a new pre-configured project for the Telerik UI for ASP.NET Core components from a project template.
Manually configuring an existing project as described in the First Steps on Windows or First Steps on Mac articles.
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>ToggleButton:</h4>
@addTagHelper *, Kendo.Mvc
<h4>ToggleButton:</h4>
2. Initialize the ToggleButton
Use the ToggleButton HtmlHelper or TagHelper to add the component to a page:
- The
Name()
configuration method is mandatory as its value is used for theid
and the name attributes of the ToggleButton element. - The
Content()
configuration specifies the text that is rendered within the button. This option accepts only string values and no HTML. - The
Enable()
configuration defines if the component is enabled or disabled. - The
Group()
configuration is to specify a commondata-group
attribute which allows access for multiple ToggleButton instances. - The
Selected()
configuration defines whether the component will be selected.
<h4>ToggleButton:</h4>
@(Html.Kendo().ToggleButton()
.Name("toggleButton")
.Group("badges")
.Selected(false)
.Enable(true)
.Content("Humidity")
)
@addTagHelper *, Kendo.Mvc
<h4>ToggleButton:</h4>
<kendo-togglebutton name="toggleButton"
group="badges"
selected="false"
enable="true">
Humidity
</kendo-togglebutton>
3. Add an Icon and a Badge
The next step is to display an icon and a badge within the ToggleButton. This allows you to bolster the visual rendering of the component.
<h4>ToggleButton:</h4>
@(Html.Kendo().ToggleButton()
.Name("toggleButton")
.Group("badges")
.Selected(false)
.Icon("droplet")
.Badge(badge => badge
.Position(BadgePosition.Edge)
.Align(BadgeAlign.TopEnd)
.Shape(BadgeShape.Rectangle)
.Text("80%")
.ThemeColor(BadgeColor.Success)
)
.Enable(true)
.Content("Humidity")
)
@addTagHelper *, Kendo.Mvc
<h4>ToggleButton:</h4>
<kendo-togglebutton name="toggleButton"
tag="a"
group="badges"
icon="droplet"
selected="false">
<badge position="BadgePosition.Edge"
align="BadgeAlign.TopEnd"
shape="BadgeShape.Rectangle"
text="80%"
theme-color="BadgeColor.Success" />
Humidity
</kendo-togglebutton>
4. Handle a ToggleButton Event
The ToggleButton exposes a Toggle()
event that you can handle and assign specific functions to the component. In this tutorial, you will use the Toggle()
event to alert for the checked state of the component.
<h4>ToggleButton:</h4>
@(Html.Kendo().ToggleButton()
.Name("toggleButton")
.Group("badges")
.Selected(false)
.Icon("droplet")
.Badge(badge => badge
.Position(BadgePosition.Edge)
.Align(BadgeAlign.TopEnd)
.Shape(BadgeShape.Rectangle)
.Text("80%")
.ThemeColor(BadgeColor.Success)
)
.Events(events => events.Toggle("onToggle"))
.Enable(true)
.Content("Humidity")
)
<script>
function onToggle(e) {
alert("Checked state:" + e.checked);
}
</script>
@addTagHelper *, Kendo.Mvc
<h4>ToggleButton:</h4>
<kendo-togglebutton name="toggleButton"
tag="a"
group="badges"
icon="droplet"
on-toggle="onToggle"
selected="false">
<badge position="BadgePosition.Edge"
align="BadgeAlign.TopEnd"
shape="BadgeShape.Rectangle"
text="80%"
theme-color="BadgeColor.Success" />
Humidity
</kendo-togglebutton>
<script>
function onToggle(e) {
alert("Checked state:" + e.checked);
}
</script>
For more examples, refer to the demo on using the events of the ToggleButton.
5. (Optional) Reference Existing ToggleButton Instances
You can reference the ToggleButton 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 togglebuttonReference = $("#toggleButton").data("kendoToggleButton"); // togglebuttonReference is a reference to the existing togglebutton instance of the helper. </script>
-
Use the ToggleButton client-side API to control the behavior of the widget. In this example, you will use the
enable
method to disable the component. The method is inherited from the Button widget.<script> var togglebuttonReference = $("#toggleButton").data("kendoToggleButton"); // togglebuttonReference is a reference to the existing togglebutton instance of the helper. togglebuttonReference.enable(false) </script>
For more information on referencing specific helper instances, see the Methods and Events article.
Explore this Tutorial in REPL
You can continue experimenting with the code sample above by running it in the Telerik REPL server playground: