ASP.NET Core ColorPicker Overview

Telerik UI for ASP.NET Core Ninja image

The ColorPicker is part of Telerik UI for ASP.NET Core, a professional grade UI library with 110+ components for building modern and feature-rich applications. To try it out sign up for a free 30-day trial.

The Telerik UI ColorPicker TagHelper and HtmlHelper for ASP.NET Core are server-side wrappers for the Kendo UI ColorPicker widget.

The ColorPicker is a drop-down list for selecting colors.

Initializing the ColorPicker

The following example demonstrates how to define the ColorPicker.

    @(Html.Kendo().ColorPicker()
          .Name("colorpicker") // The name of the ColorPicker is mandatory. It specifies the "id" attribute of the widget.
          .Value("#ff0000") // Set the value of the ColorPicker.
    )
        <kendo-colorpicker name="colorpicker" value="#ff0000">
            <messages apply="Change" cancel="Close" />
        </kendo-colorpicker>

Basic Configuration

The following example demonstrates how to configure the TileSize, columns, change event, and palette colors.


        @(Html.Kendo().ColorPalette()
            .Name("palette")
            .Columns(4)
            .TileSize(s => s.Width(34).Height(19))
            .Palette(new string[] {
                "#f0d0c9", "#e2a293", "#d4735e", "#65281a",
                "#eddfda", "#dcc0b6", "#cba092", "#7b4b3a",
                "#fcecd5", "#f9d9ab", "#f6c781", "#c87d0e",
                "#e1dca5", "#d0c974", "#a29a36", "#514d1b",
                "#c6d9f0", "#8db3e2", "#548dd4", "#17365d"
            })
            .Events(events => events.Change("preview"))
        )

        @{
            string[] colorPickerPalette = new string[] {
                                        "#f0d0c9", "#e2a293", "#d4735e", "#65281a",
                                        "#eddfda", "#dcc0b6", "#cba092", "#7b4b3a",
                                        "#fcecd5", "#f9d9ab", "#f6c781", "#c87d0e",
                                        "#e1dca5", "#d0c974", "#a29a36", "#514d1b",
                                        "#c6d9f0", "#8db3e2", "#548dd4", "#17365d"
                                    };
        }
        <kendo-colorpalette name="palette" columns="4" on-change="preview" palette-colors="colorPickerPalette">
            <tile-size height="19" width="34" />
        </kendo-colorpalette>

Functionality and Features

Events

You can subscribe to all ColorPicker events. For a complete example on basic ColorPicker events, refer to the demo on using the events of the ColorPicker.

Handling by Handler Name

The following example demonstrates how to subscribe to events by a handler name.

    @(Html.Kendo().ColorPicker()
        .Name("colorpicker")
        .Events(e => e
            .Open("colorpicker_open")
            .Close("colorpicker_close")
            .Select("colorpicker_select")
            .Change("colorpicker_change")
        )
    )
    <script>
        // The ColorPicker instance is available as an e.sender or this.
        function colorpicker_open(e) {
            // Handle the open event.
        }

        function colorpicker_close(e) {
            // Handle the close event.
        }

        function colorpicker_select(e) {
            // Handle the select event.
        }

        function colorpicker_change(e) {
            // Handle the change event.
        }
    </script>
        <kendo-colorpicker name="colorpicker" on-select="colorpicker_select" on-change="colorpicker_change" on-open="colorpicker_open" on-close="colorpicker_close">
        </kendo-colorpicker>

            <script>
        // The ColorPicker instance is available as an e.sender or this.
        function colorpicker_open(e) {
            // Handle the open event.
        }

        function colorpicker_close(e) {
            // Handle the close event.
        }

        function colorpicker_select(e) {
            // Handle the select event.
        }

        function colorpicker_change(e) {
            // Handle the change event.
        }
    </script>

Handling by Template Delegate

The following example demonstrates how to subscribe to events by a template delegate.

    @(Html.Kendo().ColorPicker()
          .Name("colorpicker")
          .Events(e => e
              .Open(@<text>
                function(e) {
                    //Handle the open event inline.
                }
              </text>)
              .Close(@<text>
                function(e) {
                    //Handle the close event inline.
                }
                </text>)
              .Select(@<text>
                function(e) {
                    //Handle the select event inline.
                }
                </text>)
              .Change(@<text>
                function(e) {
                    //Handle the change event inline.
                }
                </text>)
          )
    )

Referencing Existing Instances

To reference an existing Kendo UI ColorPicker instance, use the jQuery.data() configuration option. Once a reference is established, use the ColorPicker client-side API to control its behavior.

    // Place the following after the ColorPicker for ASP.NET Core declaration.
    <script>
    $(function() {
        // The Name() of the ColorPicker is used to get its client-side instance.
        var colorpicker = $("#colorpicker").data("kendoColorPicker");
    });
    </script>

See Also

In this article