Getting Started with the ButtonGroup
This tutorial explains how to set up a basic Telerik UI for ASP.NET Core 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. 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>ButtonGroup with text and icon</h4>
<br /><br />
<h4>ButtonGroup only with icons</h4>
@addTagHelper *, Kendo.Mvc
<h4>ButtonGroup with text and icon</h4>
<br /><br />
<h4>ButtonGroup only with icons</h4>
2. Initialize the ButtonGroup
Use the ButtonGroup 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 ButtonGroup element. - The
Items()
configuration specifies the configuration of the items that are rendered within the ButtonGroup. You will use theAdd()
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>
@addTagHelper *, Kendo.Mvc
<h4>ButtonGroup with text and icon</h4>
<kendo-buttongroup name="player">
<buttongroup-items>
<item text="Play"></item>
<item text="Pause"></item>
<item text="Stop"></item>
</buttongroup-items>
</kendo-buttongroup>
<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.
)
@addTagHelper *, Kendo.Mvc
<h4>ButtonGroup with text and icon</h4>
<kendo-buttongroup name="player">
<buttongroup-items>
<item icon="play" text="Play"></item>
<item icon="pause" text="Pause"></item>
<item icon="stop" text="Stop"></item>
</buttongroup-items>
</kendo-buttongroup>
<br /><br />
<h4>ButtonGroup only with icons</h4>
<kendo-buttongroup name="disabledPlayer" enable="false">
<buttongroup-items>
<item icon="play"></item>
<item icon="pause"></item>
<item icon="stop"></item>
</buttongroup-items>
</kendo-buttongroup>
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>
@addTagHelper *, Kendo.Mvc
<h4>ButtonGroup with text and icon</h4>
<kendo-buttongroup name="player" on-select="onSelect">
<buttongroup-items>
<item icon="play" text="Play"></item>
<item icon="pause" text="Pause"></item>
<item icon="Stop" text="Stop"></item>
</buttongroup-items>
</kendo-buttongroup>
<br /><br />
<h4>ButtonGroup only with icons</h4>
<kendo-buttongroup name="disabledPlayer" enable="false">
<buttongroup-items>
<item icon="play"></item>
<item icon="pause"></item>
<item icon="stop"></item>
</buttongroup-items>
</kendo-buttongroup>
<script>
function onSelect(e) {
console.log("Selected index: " + e.indices);
}
</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:
-
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>
-
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.
Explore this Tutorial in REPL
You can continue experimenting with the code sample above by running it in the Telerik REPL server playground:
Next Steps
- Using Icons in the ButtonGroup
- Configuring the Selection Mode in the ButtonGroup
- Configuring the Index in the ButtonGroup