Click here to Skip to main content
15,879,535 members
Articles / Web Development / ASP.NET

Change Background Color of Ajax HtmlEditorExtender using ColorPickerExtender

1 May 2014CPOL1 min read 16.3K   116   3   4
In this blog, we will learn to change the Background Color of Ajax HtmlEditorExtender using ColorPickerExtender.

[Demo] Change Background Color of HtmlEditorExtender

[Demo] Change Background Color of HtmlEditorExtender

In this blog, we will learn to change the background color of Ajax HtmlEditorExtender using ColorPickerExtender.

Background

There was an question which eventually gave birth to this blog.

Here Comes the Logic

We have already discussed the logic in one of the previous blogs Set Content inside AJAX HTMLEditor and EditorExtender using JavaScript. We just need to find out the Edit Panel, that is the div, which is actually showing data for HtmlEditorExtender. Then, we can easily assign Selected Color of ColorPickerExtender to that div.

HTML
<div contenteditable="true"
     id="txtEditorDemo$HtmlEditorExtenderBehavior_ExtenderContentEditable"
     style="height: 80%; overflow: auto; clear: both;"
     class="ajax__html_editor_extender_texteditor">
</div>

Okay! Let’s Get Our Feet Wet

Get the ColorPickerExtender Selected Color

We can easily get the Selected color by handling the OnClientColorSelectionChanged Event of ColorPickerExtender. We can call a JavaScript function on this Event. Let’s see the markup.

ASP.NET
<asp:ColorPickerExtender runat="server" 
                         TargetControlID="txtSelectColor"
                         SampleControlID="sampleBodyColor"
                         PopupButtonID="txtSelectColor" 
                         OnClientColorSelectionChanged="colorChanged">
</asp:ColorPickerExtender>

So, the JavaScript function is colorChanged(). The following code would alert you the Selected Color.

JavaScript
function colorChanged(sender) {
    alert(sender.get_selectedColor());
}

Selected Color on Alert Box

Selected Color on Alert Box

Now, Let’s Get the HtmlEditorExtender and Change Its Background Color

There are two ways to do this:

  • Using Sys.Application $find Method

    First, find the HTMLEditorExtenderusing ID, then assign the Selected Color to the Background Color Property of its EditableDiv.

    JavaScript
    var htmlEditorExtender = $find("<%= heeEditorDemo.ClientID %>");
    htmlEditorExtender._editableDiv
                      .style
                      .backgroundColor = '#' + sender.get_selectedColor();
  • Using jQuery Class Selector

    Get the HTMLEditorExtender by its class name ajax__html_editor_extender_texteditor. Then set its Background Color by .css() Method.

    JavaScript
    var htmlEditorExtender = $('.ajax__html_editor_extender_texteditor');
    htmlEditorExtender.css('background-color', '#' + sender.get_selectedColor());

NOTE

Here, we have appended ‘#’ before the Selected Color. That is because Selected Color is only a Hex Number without the prefix ‘#’, as we can see on the screenshot above, where we have alerted the Selected Color.

Thanks !!!

For taking the time and reading the blog. If you find it useful, then share within your circle, by pressing the social icons.

Image 3 Image 4

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Proud Indian | Author | TEDx Speaker | Microsoft MVP | CodeProject MVP | Speaker | DZone Most Valuable Blogger| jsfiddler

My Website

taditdash.com

Programming Community Profiles

jsfiddle | Stack Overflow

Social Profiles

Facebook | Twitter | LinkedIn

Awards


  1. DZone Most Valuable Blogger
  2. Microsoft MVP 2014, 2015, 2016, 2017, 2018
  3. Code Project MVP 2014, 2015, 2016
  4. Star Achiever of the Month December 2013
  5. Mindfire Techno Idea Contest 2013 Winner
  6. Star of the Month July 2013

Comments and Discussions

 
GeneralNice and Thanks Pin
Alexandra Micheal1-May-14 21:37
Alexandra Micheal1-May-14 21:37 
GeneralRe: Nice and Thanks Pin
Tadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)1-May-14 22:10
protectorTadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)1-May-14 22:10 
GeneralRe: Nice and Thanks Pin
Alexandra Micheal1-May-14 22:57
Alexandra Micheal1-May-14 22:57 
GeneralRe: Nice and Thanks Pin
Tadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)1-May-14 23:43
protectorTadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)1-May-14 23:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.