Getting Started with the ColorPalette
This tutorial explains how to set up a basic Telerik UI for ASP.NET MVC ColorPalette and highlights the major steps in the configuration of the component.
You will initialize a ColorPalette control with a custom set of colors. Next, you will handle the Change
event of the component to get the selected color and apply it as a background color to DropDownButtons.
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.
To manually configure an existing project by using NuGet, see the Adding Telerik UI through 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:
@using Kendo.Mvc.UI
Optionally, you can structure the View content by adding the desired HTML elements like headings, divs, paragraphs, and others.
@using Kendo.Mvc.UI
<div class="main-container">
<div class="colorpalette-container">
<h4>Select a color:</h4>
</div>
</div>
2. Initialize the ColorPalette
Use the ColorPalette HtmlHelper to add the component to a page:
- The
Name()
configuration method is mandatory as its value is used for theid
and thename
attributes of the ColorPalette element. - The
Palette()
option sets the desired range of colors that the user can pick from.
@using Kendo.Mvc.UI
<div class="main-container">
<div class="colorpalette-container">
<h4>Select a color:</h4>
@(Html.Kendo().ColorPalette()
.Name("colorpalette")
.Palette(new string[] {
"#f2f2f2", "#7f7f7f", "#a7d6ff", "#d9dde4", "#e5f5d7", "#fad0e4", "#fef0cd", "#c5f2ff", "#e2e7f4", "#c9f7f1"
})
)
</div>
</div>
3. Implement the Color Changing
The next step is to configure DropDownButtons and handle the ColorPalette color selection. The Change
event handler allows you to access the selected color. Iterate through the DropDownButtons on the page and change the background color of each button by using the jQuery css()
method.
@using Kendo.Mvc.UI
<div class="main-container">
<div class="colorpalette-container">
<h4>Select a color:</h4>
@(Html.Kendo().ColorPalette()
.Name("colorpalette")
.Palette(new string[] {
"#f2f2f2", "#7f7f7f", "#a7d6ff", "#d9dde4", "#e5f5d7", "#fad0e4", "#fef0cd", "#c5f2ff", "#e2e7f4", "#c9f7f1"
})
.Events(ev=>ev.Change("onChange")))
</div>
<div class="buttons-container">
<div class='column'>
<h4>Default Button:</h4>
@(Html.Kendo().DropDownButton()
.Name("default")
.Text("Paste")
.Icon("clipboard")
.Items(items=>{
items.Add().Id("keep-text").Text("Keep Text Only").Icon("clipboard-text").Click(@<text>function() { alert("Keep Text Only") }</text>);
items.Add().Id("paste-html").Text("Paste as HTML").Icon("clipboard-code").Click(@<text>function() { alert("Paste as HTML") }</text>);
items.Add().Id("paste-markdown").Text("Paste Markdown").Icon("clipboard-markdown").Click(@<text>function() { alert("Paste Markdown") }</text>);
items.Add().Id("paste-default").Text("Set Default Paste").Click(@<text>function() { alert("Set Default Paste") }</text>);
})
)
</div>
<div class='column'>
<h4>Outline Button:</h4>
@(Html.Kendo().DropDownButton()
.Name("outline")
.Text("Paste")
.FillMode(FillMode.Outline)
.Icon("clipboard")
.Items(items=>{
items.Add().Id("keep-text").Text("Keep Text Only").Icon("clipboard-text").Click(@<text>function() { alert("Keep Text Only") }</text>);
items.Add().Id("paste-html").Text("Paste as HTML").Icon("clipboard-code").Click(@<text>function() { alert("Paste as HTML") }</text>);
items.Add().Id("paste-markdown").Text("Paste Markdown").Icon("clipboard-markdown").Click(@<text>function() { alert("Paste Markdown") }</text>);
items.Add().Id("paste-default").Text("Set Default Paste").Click(@<text>function() { alert("Set Default Paste") }</text>);
})
)
</div>
<div class='column'>
<h4>Primary Button:</h4>
@(Html.Kendo().DropDownButton()
.Name("primary")
.Text("Paste")
.ThemeColor(ThemeColor.Primary)
.Icon("clipboard")
.Items(items=>{
items.Add().Id("keep-text").Text("Keep Text Only").Icon("clipboard-text").Click(@<text>function() { alert("Keep Text Only") }</text>);
items.Add().Id("paste-html").Text("Paste as HTML").Icon("clipboard-code").Click(@<text>function() { alert("Paste as HTML") }</text>);
items.Add().Id("paste-markdown").Text("Paste Markdown").Icon("clipboard-markdown").Click(@<text>function() { alert("Paste Markdown") }</text>);
items.Add().Id("paste-default").Text("Set Default Paste").Click(@<text>function() { alert("Set Default Paste") }</text>);
})
)
</div>
</div>
</div>
<script>
function onChange(e){
let selectedColor = e.value;
$.each($("[data-role='dropdownbutton']"), function(){
$(this).css("background-color", selectedColor)
});
}
</script>
4. Customize the Appearance of ColorPalette
You can customize the overall ColorPalette appearance by using the following options:
- Use the
Columns()
method to specify the required number of columns. - The
TileSize()
configuration allows you to define the size of the color cell.
@using Kendo.Mvc.UI
<div class="main-container">
<div class="colorpalette-container">
<h4>Select a color:</h4>
@(Html.Kendo().ColorPalette()
.Name("colorpalette")
.Palette(new string[] {
"#f2f2f2", "#7f7f7f", "#a7d6ff", "#d9dde4", "#e5f5d7", "#fad0e4", "#fef0cd", "#c5f2ff", "#e2e7f4", "#c9f7f1"
})
.Columns(4)
.TileSize(ts => ts.Width(50).Height(50))
)
</div>
</div>
5. (Optional) Reference Existing ColorPalette Instances
You can reference the ColorPalette instances that you have created and build on top of their existing configuration:
-
Use the value of the
Name()
option of the component to establish a reference.<script> $(document).ready(function() { var colorPaletteReference = $("#colorpalette").data("kendoColorPalette"); // colorPaletteReference is a reference to the existing ColorPalette instance of the helper. }); </script>
-
Use the ColorPalette client-side API to control the behavior of the component. In this example, you will use the
enable
method to disable the control dynamically (for example, when a button is clicked).@(Html.Kendo().Button() .Name("btn") .Content("Disable ColorPalette") .Events(ev => ev.Click("onBtnClick")))
<script> function onBtnClick() { var colorPaletteReference = $("#colorpalette").data("kendoColorPalette"); colorPaletteReference.enable(false); } </script>
Next Steps
- Configuring Color Presets of the ColorPalette
- Using the Keyboard Navigation of the ColorPalette for ASP.NET MVC (Demo)