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

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

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>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.
    @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 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.

    @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.

    @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.

    <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 to control the behavior of the widget. In this example, you will use the enable method to disable the button.

    <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 article.

Next Steps

See Also

In this article