# Getting Started with the ScrollView

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

You will initialize a ScrollView and learn how to scroll to a view. Then, you will see how to attach an event handler to the component.

![Sample Telerik UI for ASP.NET MVC ScrollView](https://www.telerik.com/aspnet-mvc/documentation/assets/888984ac851770b333e32b0c06ea38a8/scrollview-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>ScrollView with event handler</h4>
<p>
</p>
```

## 2. Initialize the ScrollView

Use the ScrollView 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 ScrollView element.
* The `ContentHeight()` configuration method sets the height of the ScrollView content.
* The `Items()` configuration method sets the items in the ScrollView.

```Razor HtmlHelper
@using Kendo.Mvc.UI
<h4>ScrollView with event handler</h4>
<p>
    @(Html.Kendo().ScrollView()
                .Name("scrollView")
                .EnablePager(false)
                .ContentHeight("100%")
                .TemplateId("scrollview-template")
                .DataSource(d =>
                        d.Custom()
                          .Type("odata-v4")
                          .Transport(t => t.Read(r => r.Url("https://demos.telerik.com/service/v2/odata/Products")))
                          .ServerPaging(true)
                          .PageSize(3))
                .HtmlAttributes(new { style = "height:500px; width:890px; max-width: 100%;" })
    )
</p>

<script id="scrollview-template" type="text/x-kendo-template">
    <div class="img-wrapper">
        # for (var i = 0; i < data.length; i++) { #
        <div>
            <div style="width: 140px; height: 140px;  background-image: #=setBackground(data[i].ProductID)#; background-repeat:no-repeat; background-size: cover;"></div>
            <p>#= data[i].ProductName #</p>
        </div>
        # } #
    </div>
</script>
<script>
    function setBackground(id) {
        return "url(https://demos.telerik.com/kendo-ui/content/web/foods/" + id + ".jpg)";
    }
</script>

<style>
    .k-scrollview-wrap .img-wrapper {
        display: flex;
        width: 100%;
        height: 100%;
        vertical-align: middle;
        align-items: center;
        justify-content: space-around;
        text-align: center;
    }

    .k-scrollview-wrap .img-wrapper>div {
        width: 30%;
        min-width: 150px;
        box-sizing: border-box;
        display: inline-block;
        vertical-align: top;
        margin-bottom: 1em;
    }

    .k-scrollview-wrap .img-wrapper>div>div {
        margin: auto;
        margin-bottom: 0.5em;
    }
</style>
```

## 3. Use the Duration Configuration of the ScrollView

The next step is to configure Duration configuration. The `Duration` (in milliseconds) for the ScrollView is used to snap to the current page after the user releases it.

```Razor HtmlHelper
@using Kendo.Mvc.UI
<h4>ScrollView with event handler</h4>
<p>
    @(Html.Kendo().ScrollView()
                .Name("scrollView")
                .EnablePager(false)
                .ContentHeight("100%")
                .Duration(1500)
                .TemplateId("scrollview-template")
                .DataSource(d =>
                        d.Custom()
                          .Type("odata-v4")
                          .Transport(t => t.Read(r => r.Url("https://demos.telerik.com/service/v2/odata/Products")))
                          .ServerPaging(true)
                          .PageSize(3))
                .HtmlAttributes(new { style = "height:500px; width:890px; max-width: 100%;" })
    )
</p>

<script id="scrollview-template" type="text/x-kendo-template">
    <div class="img-wrapper">
        # for (var i = 0; i < data.length; i++) { #
        <div>
            <div style="width: 140px; height: 140px;  background-image: #=setBackground(data[i].ProductID)#; background-repeat:no-repeat; background-size: cover;"></div>
            <p>#= data[i].ProductName #</p>
        </div>
        # } #
    </div>
</script>
<script>
    function setBackground(id) {
        return "url(https://demos.telerik.com/kendo-ui/content/web/foods/" + id + ".jpg)";
    }
</script>

<style>
    .k-scrollview-wrap .img-wrapper {
        display: flex;
        width: 100%;
        height: 100%;
        vertical-align: middle;
        align-items: center;
        justify-content: space-around;
        text-align: center;
    }

    .k-scrollview-wrap .img-wrapper>div {
        width: 30%;
        min-width: 150px;
        box-sizing: border-box;
        display: inline-block;
        vertical-align: top;
        margin-bottom: 1em;
    }

    .k-scrollview-wrap .img-wrapper>div>div {
        margin: auto;
        margin-bottom: 0.5em;
    }
</style>
```

## 4. Handle a ScrollView Event

The ScrollView exposes a `Change()` event that you can handle and assign specific functions to the component. In this tutorial, you will use the `Change()` event to display a message when the page is changed.

```Razor HtmlHelper
@using Kendo.Mvc.UI
<h4>ScrollView with event handler</h4>
<p>
    @(Html.Kendo().ScrollView()
                .Name("scrollView")
                .EnablePager(false)
                .ContentHeight("100%")
                .Duration(1500)
                .TemplateId("scrollview-template")
                .DataSource(d =>
                        d.Custom()
                          .Type("odata-v4")
                          .Transport(t => t.Read(r => r.Url("https://demos.telerik.com/service/v2/odata/Products")))
                          .ServerPaging(true)
                          .PageSize(3))
                .HtmlAttributes(new { style = "height:500px; width:890px; max-width: 100%;" })
                .Events(e => e.Change("onChange"))
    )
</p>

<script id="scrollview-template" type="text/x-kendo-template">
    <div class="img-wrapper">
        # for (var i = 0; i < data.length; i++) { #
        <div>
            <div style="width: 140px; height: 140px;  background-image: #=setBackground(data[i].ProductID)#; background-repeat:no-repeat; background-size: cover;"></div>
            <p>#= data[i].ProductName #</p>
        </div>
        # } #
    </div>
</script>
<script>
    function setBackground(id) {
        return "url(https://demos.telerik.com/kendo-ui/content/web/foods/" + id + ".jpg)";
    }

    function onChange(){
        console.log("change");
    }
</script>

<style>
    .k-scrollview-wrap .img-wrapper {
        display: flex;
        width: 100%;
        height: 100%;
        vertical-align: middle;
        align-items: center;
        justify-content: space-around;
        text-align: center;
    }

    .k-scrollview-wrap .img-wrapper>div {
        width: 30%;
        min-width: 150px;
        box-sizing: border-box;
        display: inline-block;
        vertical-align: top;
        margin-bottom: 1em;
    }

    .k-scrollview-wrap .img-wrapper>div>div {
        margin: auto;
        margin-bottom: 0.5em;
    }
</style>
```

For more examples, refer to the [demo on using the events of the ScrollView](https://demos.telerik.com/aspnet-mvc/scrollview/events).

## 5. (Optional) Reference Existing ScrollView Instances

You can reference the ScrollView 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 scrollView = $("#scrollView").kendoScrollView().data().kendoScrollView; // scrollView is a reference to the existing scrollView instance of the helper.
   </script>
   ```
2. Use the [ScrollView client-side API](https://docs.telerik.com/kendo-ui/api/ui/scrollview#methods) to control the behavior of the widget. In this example, you will use the `next` method to switch to the next page with an animation.
   ```JS script
   <script>
       var scrollView = $("#scrollView").kendoScrollView().data().kendoScrollView; // scrollView is a reference to the existing scrollView instance of the helper.
       scrollView.next();
   </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.

## 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 ScrollView HtmlHelper](https://netcorerepl.telerik.com/cxbQlpbS41C5RHBh28)

## Next Steps

* [Use Paging for the ScrollView](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/media/scrollview/paginig)
* [Use Templates for the ScrollView](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/media/scrollview/templates)
* [Use Keyboard Navigation for the ScrollView](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/media/scrollview/keyboard-navigation)

## See Also

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