# Getting Started with the ButtonGroup

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

You will initialize two button groups, one of them will have an event handler. The other one will be rendered as disabled.

![Sample Telerik UI for ASP.NET MVC Button](https://www.telerik.com/aspnet-mvc/documentation/assets/506cc1649e97984780afd6e32580a8cb/buttongroup-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 MVC components:

* To create a new pre-configured project for the Telerik UI for ASP.NET MVC components, you can [use a project template](https://www.telerik.com/aspnet-mvc/documentation/getting-started-mvc/first-steps).

* To manually configure an existing project by using NuGet, see the [Adding Telerik UI through NuGet](https://www.telerik.com/aspnet-mvc/documentation/getting-started/setup-with-nuget).

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

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

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>ButtonGroup with text and icon</h4>

    <br /><br />
    <h4>ButtonGroup only with icons</h4>
```

## 2. Initialize the ButtonGroup

Use the ButtonGroup HtmlHelper  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 ButtonGroup element.
* The `Items()` configuration specifies the configuration of the items that are rendered within the ButtonGroup. You will use the `Add()` method to define three items: Play, Pause, and Stop.
* The `Text()` configuration method sets the text for each ButtonGroup item.

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

    <h4>ButtonGroup with text and icon</h4>
    @(Html.Kendo().ButtonGroup()
        .Name("player")
        .Items(t => // Add the three ButtonGroup items.
        {
            t.Add().Text("Play");
            t.Add().Text("Pause");
            t.Add().Text("Stop");
        })
    )
    <br /><br />
    <h4>ButtonGroup only with icons</h4>
```

## 3. Add Icons

The next step is to display [icons](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/navigation/buttongroup/icons) for the items in the ButtonGroup. This allows you to enhance their visual representation. You will also add the second ButtonGroup component that will have only icons without text and will be rendered as disabled.

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

    <h4>ButtonGroup with text and icon</h4>
    @(Html.Kendo().ButtonGroup()
        .Name("player")
        .Items(t =>
        {
            t.Add().Text("Play").Icon("play"); // Use the Icon method to render an icon in each ButtonGroup item.
            t.Add().Text("Pause").Icon("pause");
            t.Add().Text("Stop").Icon("stop");
        })
    )
    <br /><br />
    <h4>ButtonGroup only with icons</h4>
    @(Html.Kendo().ButtonGroup() // Initialize the second ButtonGroup component.
        .Name("disabledPlayer")
        .Items(t =>
        {
            t.Add().Icon("play");
            t.Add().Icon("pause");
            t.Add().Icon("stop");
        })
        .Enable(false) // Render the ButtonGroup component as disabled.
    )
```

## 4. Handle a ButtonGroup Event

The ButtonGroup exposes the `Select()` event that you can handle and assign specific functions to the component. In this tutorial, you will use the `Select()` event to display the index of the selected button (0, 1, or 2) in the browser console.

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

    <h4>ButtonGroup with text and icon</h4>
    @(Html.Kendo().ButtonGroup()
        .Name("player")
        .Items(t =>
        {
            t.Add().Text("Play").Icon("play");
            t.Add().Text("Pause").Icon("pause");
            t.Add().Text("Stop").Icon("stop");
        })
        .Events(ev => ev.Select("onSelect")) // Use the Select() event of the ButtonGroup.
    )
    <br /><br />
    <h4>ButtonGroup only with icons</h4>
    @(Html.Kendo().ButtonGroup()
        .Name("disabledPlayer")
        .Items(t =>
        {
            t.Add().Icon("play");
            t.Add().Icon("pause");
            t.Add().Icon("stop");
        })
        .Enable(false)
    )

    <script>
        function onSelect(e) {
            console.log("Selected index: " + e.indices); // Display the index of the selected button in the browser console.
        }
    </script>
```

## 5. (Optional) Reference Existing ButtonGroup Instances

You can reference the ButtonGroup 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 buttonGroupReference = $("#player").data("kendoButtonGroup"); // buttonGroupReference is a reference to the existing player instance of the helper.
   </script>
   ```

2. Use the [ButtonGroup client-side API](https://docs.telerik.com/kendo-ui/api/ui/buttongroup#methods) to control the behavior of the widget. In this example, you will use the `enable` method to disable the button.

   ```JS script
   <script>
       var buttonGroupReference = $("#player").data("kendoButtonGroup"); // buttonGroupReference is a reference to the existing player instance of the helper.
       buttonGroupReference.enable(false); // Disable the ButtonGroup.
   </script>
   ```

For more information on referencing specific helper instances, see the [Methods and Events](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/helper-basics/events-and-methods) article.

## Next Steps

* [Using Icons in the ButtonGroup](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/navigation/buttongroup/icons)
* [Configuring the Selection Mode in the ButtonGroup](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/navigation/buttongroup/selection)
* [Configuring the Index in the ButtonGroup](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/navigation/buttongroup/indexes)

## See Also

* [Using the API of the ButtonGroup for ASP.NET MVC (Demo)](https://demos.telerik.com/aspnet-mvc/buttongroup/api)
* [ButtonGroup Client-Side API](https://docs.telerik.com/kendo-ui/api/ui/buttongroup)
* [Server-Side API of the ButtonGroup for ASP.NET MVC](https://www.telerik.com/aspnet-mvc/documentation/api/buttongroup)
* [Knowledge Base Section](https://www.telerik.com/aspnet-mvc/documentation/knowledge-base)
