# Getting Started with the SegmentedControl

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

You will initialize a SegmentedControl with several items, configure icons, set a default selection, and handle the `Change` event. Finally, you can run the sample code in [Telerik REPL](https://netcorerepl.telerik.com/) 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](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:

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

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

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

## 2. Initialize the SegmentedControl

Use the SegmentedControl 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 SegmentedControl element.

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

@(Html.Kendo().SegmentedControl()
    .Name("segmentedControl")
)
```

```Razor TagHelper
@addTagHelper *, Kendo.Mvc

<kendo-segmentedcontrol name="segmentedControl">
</kendo-segmentedcontrol>
```

## 3. Add Items

Use the [`Items()`](https://www.telerik.com/aspnet-core-ui/documentation/api/kendo.mvc.ui.fluent/segmentedcontrolbuilder#itemssystemactionkendomvcuifluentsegeentedcontrolitemfactory) configuration to define the selectable segments. Each item requires a `Text()` and a `Value()`.

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

@(Html.Kendo().SegmentedControl()
    .Name("segmentedControl")
    .Items(items =>
    {
        items.Add().Text("Day").Value("day");
        items.Add().Text("Week").Value("week");
        items.Add().Text("Month").Value("month");
    })
)
```

```Razor TagHelper
@addTagHelper *, Kendo.Mvc

<kendo-segmentedcontrol name="segmentedControl">
    <items>
        <item text="Day" value="day"></item>
        <item text="Week" value="week"></item>
        <item text="Month" value="month"></item>
    </items>
</kendo-segmentedcontrol>
```

## 4. Add Icons

Enhance the visual representation of the items by adding icons with the [`Icon()`](https://www.telerik.com/aspnet-core-ui/documentation/api/kendo.mvc.ui.fluent/segmentedcontrolitembuilder#iconsystemstring) option.

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

@(Html.Kendo().SegmentedControl()
    .Name("segmentedControl")
    .Items(items =>
    {
        items.Add().Text("Day").Value("day").Icon("calendar");
        items.Add().Text("Week").Value("week").Icon("clock");
        items.Add().Text("Month").Value("month").Icon("calendar-date");
    })
)
```

```Razor TagHelper
@addTagHelper *, Kendo.Mvc

<kendo-segmentedcontrol name="segmentedControl">
    <items>
        <item text="Day" value="day" icon="calendar"></item>
        <item text="Week" value="week" icon="clock"></item>
        <item text="Month" value="month" icon="calendar-date"></item>
    </items>
</kendo-segmentedcontrol>
```

## 5. Set a Default Selection

Use `SelectedValue()` to pre-select one of the items when the component renders.

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

@(Html.Kendo().SegmentedControl()
    .Name("segmentedControl")
    .Items(items =>
    {
        items.Add().Text("Day").Value("day").Icon("calendar");
        items.Add().Text("Week").Value("week").Icon("clock");
        items.Add().Text("Month").Value("month").Icon("calendar-date");
    })
    .SelectedValue("week")
)
```

```Razor TagHelper
@addTagHelper *, Kendo.Mvc

<kendo-segmentedcontrol name="segmentedControl" selected-value="week">
    <items>
        <item text="Day" value="day" icon="calendar"></item>
        <item text="Week" value="week" icon="clock"></item>
        <item text="Month" value="month" icon="calendar-date"></item>
    </items>
</kendo-segmentedcontrol>
```

## 6. Handle the Change Event

Use the `Events()` configuration to subscribe to the [`Change`](https://www.telerik.com/aspnet-core-ui/documentation/api/kendo.mvc.ui.fluent/segmentedcontroleventbuilder#changesystemstring) event and react when the user selects a different segment.

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

@(Html.Kendo().SegmentedControl()
    .Name("segmentedControl")
    .Items(items =>
    {
        items.Add().Text("Day").Value("day").Icon("calendar");
        items.Add().Text("Week").Value("week").Icon("clock");
        items.Add().Text("Month").Value("month").Icon("calendar-date");
    })
    .SelectedValue("week")
    .Events(ev => ev.Change("onChange"))
)

<script>
    function onChange(e) {
        console.log("Selected value: " + e.value);
    }
</script>
```

```Razor TagHelper
@addTagHelper *, Kendo.Mvc

<kendo-segmentedcontrol name="segmentedControl" selected-value="week" on-change="onChange">
    <items>
        <item text="Day" value="day" icon="calendar"></item>
        <item text="Week" value="week" icon="clock"></item>
        <item text="Month" value="month" icon="calendar-date"></item>
    </items>
</kendo-segmentedcontrol>

<script>
    function onChange(e) {
        console.log("Selected value: " + e.value);
    }
</script>
```

## Next Steps

* [Referencing Existing Component Instances](https://docs.telerik.com/kendo-ui/intro/widget-basics/events-and-methods#referencing-existing-component-instances)
* [Demo Page for the SegmentedControl](https://demos.telerik.com/aspnet-core/segmentedcontrol)

## See Also

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