# 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](https://netcorerepl.telerik.com/) and continue exploring the components.

![Sample Telerik UI for ASP.NET Core ToggleButton](https://www.telerik.com/aspnet-core-ui/documentation/assets/333038e9f45dc10148121019a899963b/togglebutton-getting-started.png)

## 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](https://netcorerepl.telerik.com/) 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](https://www.telerik.com/aspnet-core-ui/documentation/vs-integration/new-project-wizard).

## 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:

  ```cshtml
  @using Kendo.Mvc.UI
  ```

* To use the Telerik UI for ASP.NET Core TagHelpers:

  ```cshtml
  @addTagHelper *, Kendo.Mvc
  ```

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

```Razor HtmlHelper
    @using Kendo.Mvc.UI

    <h4>ToggleButton:</h4>
```

```Razor TagHelper
    @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 the `id` 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 common `data-group` attribute which allows access for multiple ToggleButton instances.
* The `Selected()` configuration defines whether the component will be selected.

```Razor HtmlHelper
    <h4>ToggleButton:</h4>

    @(Html.Kendo().ToggleButton()
        .Name("toggleButton")
        .Group("badges")
        .Selected(false)
        .Enable(true)
        .Content("Humidity")
    )
```

```Razor TagHelper
    @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](https://www.telerik.com/aspnet-core-ui/documentation/html-helpers/navigation/togglebutton/icons) and a [badge](https://www.telerik.com/aspnet-core-ui/documentation/html-helpers/navigation/togglebutton/badges) within the ToggleButton. This allows you to bolster the visual rendering of the component.

```Razor HtmlHelper
    <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")
    )
```

```Razor TagHelper
    @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.

```Razor HtmlHelper
    <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>
```

```Razor TagHelper
    @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](https://demos.telerik.com/aspnet-core/togglebutton/events).

## 5. (Optional) Reference Existing ToggleButton Instances

You can reference the ToggleButton 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.

   ```JS script
   <script>
       var togglebuttonReference = $("#toggleButton").data("kendoToggleButton"); // togglebuttonReference is a reference to the existing togglebutton instance of the helper.
   </script>
   ```

2. Use the [ToggleButton client-side API](https://docs.telerik.com/kendo-ui/api/ui/togglebutton#methods) 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.

   ```JS script
   <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](https://www.telerik.com/aspnet-core-ui/documentation/html-helpers/helper-basics/events-and-methods) article.

## Explore this Tutorial in REPL

You can continue experimenting with the code sample above by running it in the Telerik REPL server playground:

* [Sample code with the ToggleButton HtmlHelper](https://netcorerepl.telerik.com/cSkIvZaW35sRvYdR06)
* [Sample code with the ToggleButton TagHelper](https://netcorerepl.telerik.com/meOyFXEM347Lklw751)

## Next Steps

* [Customize the Appearance of the ToggleButton](https://www.telerik.com/aspnet-core-ui/documentation/html-helpers/navigation/togglebutton/appearance)
* [Configure the ToggleButton's Badge](https://www.telerik.com/aspnet-core-ui/documentation/html-helpers/navigation/togglebutton/badges)

## See Also

* [Using the API of the ToggleButton for ASP.NET Core (Demo)](https://demos.telerik.com/aspnet-core/togglebutton/api)

* [ToggleButton Client-Side API](https://docs.telerik.com/kendo-ui/api/ui/togglebutton)

* [Server-Side API of the ToggleButton](https://www.telerik.com/aspnet-core-ui/documentation/api/togglebutton)

* [TagHelper Server-Side API of the ToggleButton](https://docs.telerik.com/api/taghelpers/togglebutton)

* [Knowledge Base Section](https://www.telerik.com/aspnet-core-ui/documentation/knowledge-base)
