AJAX ColorPickerExtender

The AJAX ColorPickerExtender provides client-side color-picking functionality with a UI in a popup control. It can be attached to any ASP.NET TextBox control.
Properties of AJAX ColorPickerExtender
  • targetcontrolid: It is used for the TextBox Control that will display the hexadecimal color value.

  • samplecontrolid: It is used for the Control that will show the preview of the selected color.

  • PopupButtonID: It is used for the ID of a control to show the ColorPicker popup when clicked.

  • PopupPosition: It indicates where the color picker popup appears, like at the BottomLeft (default).

  • BottomRight, TopLeft, TopRight,Left or Right of the TextBox.

  • OnClientColorSelectionChanged: The JavaScript function to be called when the color is changed.
Now we will create and use an AJAX ColorPickerExtender in ASP.NET.
Step 1: Open Visual Studio and create a project named AJAXColorPickerExtender.
Step 2: Add one Webform named ColorPicker.
Step 3: Now in design view we will add a ToolkitScriptManager from the ToolBox.
  1. <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
  2. </asp:ToolkitScriptManager>
Step 4: Now we will add a TextBox that contains the Hexadecimal value of the color and a Button to trigger the color picker. We will also add a DIV in which we can view a preview of the colors.
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ColorPicker.aspx.cs" Inherits="AJAXColorPickerExtender.ColorPicker" %>
  2. <%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>
  3. <!DOCTYPE html>
  4. <html xmlns="http://www.w3.org/1999/xhtml">
  5. <head runat="server">
  6. <title></title>
  7. </head>
  8. <body>
  9. <form id="form1" runat="server">
  10. <div>
  11. <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
  12. </asp:ToolkitScriptManager>
  13. <asp:TextBox ID="tbcolor" runat="server"></asp:TextBox>
  14. <br />
  15. <br />
  16. <div id="view" style="width:60px; height:60px; border:1px solid #000;margin:0 3px;float:left">
  17. </div>
  18. <asp:Button ID="btncolor" runat="server" Text="SelectColor" />
  19. </div>
  20. </form>
  21. </body>
  22. </html>
Now the Design View looks like this.
Step 5: Now we will add the AJAXColorPickerExtender from the AJAX panel from the ToolBox and we will provide some properties for it.
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ColorPicker.aspx.cs" Inherits="AJAXColorPickerExtender.ColorPicker" %>
  2. <%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>
  3. <!DOCTYPE html>
  4. <html xmlns="http://www.w3.org/1999/xhtml">
  5. <head runat="server">
  6. <title></title>
  7. </head>
  8. <body>
  9. <form id="form1" runat="server">
  10. <div>
  11. <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
  12. </asp:ToolkitScriptManager>
  13. <asp:TextBox ID="tbcolor" runat="server"></asp:TextBox>
  14. <br />
  15. <br />
  16. <div id="view" style="width:60px; height:60px; border:1px solid #000;margin:0 3px;float:left">
  17. </div>
  18. <asp:Button ID="btncolor" runat="server" Text="SelectColor" />
  19. <asp:ColorPickerExtender ID="ColorPickerExtender" runat="server"
  20. TargetControlID="tbcolor"
  21. SampleControlID="view"
  22. PopupButtonID="btncolor"
  23. PopupPosition="Right"
  24. OnClientColorSelectionChanged="Color_Change">
  25. </asp:ColorPickerExtender>
  26. </div>
  27. </form>
  28. </body>
  29. </html>
Step 6: Now On the OnClientColorSelectionChanged event we call the Color_Change JavaScript function.
  1. <script type="text/javascript">
  2. function Color_Change(sender){
  3. sender.get_element().value = "#" + sender.get_selectedColor();
  4. }
  5. </script>
The function adds a Hash (#) as prefix to the color hexadecimal value in the TextBox.
Step 7: Now the complete code will be like this.
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ColorPicker.aspx.cs" Inherits="AJAXColorPickerExtender.ColorPicker" %>
  2. <%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>
  3. <!DOCTYPE html>
  4. <html xmlns="http://www.w3.org/1999/xhtml">
  5. <head runat="server">
  6. <title></title>
  7. <script type="text/javascript">
  8. function Color_Change(sender){
  9. sender.get_element().value = "#" + sender.get_selectedColor();
  10. }
  11. </script>
  12. </head>
  13. <body>
  14. <form id="form1" runat="server">
  15. <div>
  16. <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
  17. </asp:ToolkitScriptManager>
  18. <asp:TextBox ID="tbcolor" runat="server"></asp:TextBox>
  19. <br />
  20. <br />
  21. <div id="view" style="width:60px; height:60px; border:1px solid #000;margin:0 3px;float:left">
  22. </div>
  23. <asp:Button ID="btncolor" runat="server" Text="SelectColor" />
  24. <asp:ColorPickerExtender ID="ColorPickerExtender" runat="server"
  25. TargetControlID="tbcolor"
  26. SampleControlID="view"
  27. PopupButtonID="btncolor"
  28. PopupPosition="Right"
  29. OnClientColorSelectionChanged="Color_Change">
  30. </asp:ColorPickerExtender>
  31. </div>
  32. </form>
  33. </body>
  34. </html>
Now we will run project and see the output.
This is all about AJAX ColorPickerExtender in ASP.NET.
Thank you and happy coding.