5,666,132 members and growing! (18,310 online)
Email Password   helpLost your password?
Web Development » Custom Controls » General     Intermediate License: The Code Project Open License (CPOL)

ASP.NET Color Picker Web Server Control

By Viktar Karpach

An ASP.NET color picker web server control.
Javascript, C# (C# 2.0, C#), .NET (.NET, .NET 2.0), ASP.NET, Dev

Posted: 20 Jul 2008
Updated: 20 Jul 2008
Views: 7,328
Bookmarked: 32 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
17 votes for this Article.
Popularity: 5.31 Rating: 4.31 out of 5
1 vote, 5.9%
1
0 votes, 0.0%
2
2 votes, 11.8%
3
3 votes, 17.6%
4
11 votes, 64.7%
5

Introduction

It is difficult to find a decent color picker control for ASP.NET. However, there are plenty of pure JavaScript color picker controls. I decided to take one of them and convert it into an ASP.NET web server control. As a base, I took the dhtmlgoodies advanced color picker.

Project Setup

First, let’s do New Project ->ASP.NET Server Control. By default, the name of the project would be the same as the name of the default namespace. I called my project CutomControls, and renamed ServerControl1.cs to ColorPicker.cs. Then, I added to the project the images, JavaScript, and styles supplied by dhtmlgoodies.

CustomControlSolutionFiles.gif

Then, I clicked on each file's properties and changed the Build Action from Content to Embedded Resource.

AssemblyInfo.cs

I registered all the resources like this:

[assembly: System.Web.UI.WebResource("CustomControls.Images.slider_handle.gif", 
                                     "img/gif")]
[assembly: System.Web.UI.WebResource("CustomControls.Images.tab_center_active.gif", 
                                     "img/gif")]
[assembly: System.Web.UI.WebResource("CustomControls.Images.tab_left_active.gif", 
                                     "img/gif")]
[assembly: System.Web.UI.WebResource("CustomControls.Images.tab_left_inactive.gif", 
                                     "img/gif")]
[assembly: System.Web.UI.WebResource("CustomControls.Images.tab_right_active.gif", 
                                     "img/gif")]
[assembly: System.Web.UI.WebResource("CustomControls.Images.tab_right_inactive.gif", 
                                     "img/gif")]
[assembly: System.Web.UI.WebResource("CustomControls.Images.color_picker_icon.jpg", 
                                     "img/jpeg")]
 
 
[assembly: System.Web.UI.WebResource("CustomControls.Styles.js_color_picker_v2.css", 
                                     "text/css")]
 
[assembly: System.Web.UI.WebResource("CustomControls.Javascript.color_functions.js", 
                                     "text/js")]
[assembly: System.Web.UI.WebResource("CustomControls.Javascript.js_color_picker_v2.js", 
                                     "text/js")]

As you might have noticed, System.Web.UI.WebResource, the first parameter has the following signature:

[Assembly Name].[Folder].[File Name]

This is very important, since it is not documented even in MSDN.

ColorPicker.cs

I modified ToolboxData to look like this:

[ToolboxData("<{0}:ColorPicker runat="server">")]

Next, I wanted a custom icon in the Visual Studio Toolbox. After ToolboxData, I added the following line:

[System.Drawing.ToolboxBitmap(typeof(ColorPicker), 
                    "Images.color_picker_icon.jpg")]

where the first parameter is the type of the control and the second parameter is the icon file name used in AssemblyInfo.cs.

Then, I needed to load the stored resources from the DLL. The best event for this is OnInit:

// Javascript
string colorFunctions = Page.ClientScript.GetWebResourceUrl(typeof(ColorPicker), 
 "CustomControls.Javascript.color_functions.js");
Page.ClientScript.RegisterClientScriptInclude("color_functions.js", colorFunctions);
string colorPicker = Page.ClientScript.GetWebResourceUrl(typeof(ColorPicker), 
 "CustomControls.Javascript.js_color_picker_v2.js");
Page.ClientScript.RegisterClientScriptInclude("js_color_picker_v2.js", colorPicker);
 
//Images we need to set javascript variables, 
//so javascript will know where to find images
string script = string.Format(@"
var form_widget_amount_slider_handle = '{0}';
var tab_right_active = '{1}';
var tab_right_inactive = '{2}';
var tab_left_active = '{3}';
var tab_left_inactive = '{4}';
", Page.ClientScript.GetWebResourceUrl(typeof(ColorPicker), 
   "CustomControls.Images.slider_handle.gif")
 , Page.ClientScript.GetWebResourceUrl(typeof(ColorPicker), 
   "CustomControls.Images.tab_right_active.gif")
 , Page.ClientScript.GetWebResourceUrl(typeof(ColorPicker), 
   "CustomControls.Images.tab_right_inactive.gif")
 , Page.ClientScript.GetWebResourceUrl(typeof(ColorPicker), 
   "CustomControls.Images.tab_left_active.gif")
 , Page.ClientScript.GetWebResourceUrl(typeof(ColorPicker), 
   "CustomControls.Images.tab_left_inactive.gif")
);
 
Page.ClientScript.RegisterStartupScript(Page.GetType(), 
        "initColorPicker", script, true);
 
// CSS
HtmlGenericControl csslink = newHtmlGenericControl("link");
csslink.Attributes.Add("href", Page.ClientScript.GetWebResourceUrl(typeof(ColorPicker), 
 "CustomControls.Styles.js_color_picker_v2.css"));
csslink.Attributes.Add("type", "text/css");
csslink.Attributes.Add("rel", "stylesheet");            
Page.Header.Controls.Add(csslink);

The rest is simple. I used the RenderContents event to render the control HTML and the control is ready.

License

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

About the Author

Viktar Karpach



Occupation: Web Developer
Company: Legacy.com
Location: United States United States

Other popular Custom Controls articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 30 (Total in Forum: 30) (Refresh)FirstPrevNext
GeneralFirefox? issuememberMember 431275212:22 17 Nov '08  
GeneralRe: Firefox? issuememberViktar Karpach19:20 17 Nov '08  
GeneralPls Help Mememberfarzaneh ansary3:56 15 Nov '08  
GeneralRe: Pls Help MememberViktar Karpach13:26 15 Nov '08  
Generalthanks alotmemberfarzaneh ansary21:44 15 Nov '08  
GeneralHow to use the slidermemberMember 431275213:41 10 Nov '08  
GeneralRe: How to use the slidermemberMember 431275213:43 10 Nov '08  
GeneralUsing the control within SharePointmemberMember 37015879:12 17 Oct '08  
GeneralRe: Using the control within SharePointmemberViktar Karpach14:48 17 Oct '08  
GeneralUpdated valuememberMember 106667523:20 15 Oct '08  
GeneralRe: Updated valuememberViktar Karpach15:08 17 Oct '08  
Answerthank you very muchmemberHowdy Nun1:10 9 Oct '08  
Generalproblem resolved: how to use the control programaticallymemberpixel3cs2:07 7 Sep '08  
GeneralRe: problem resolved: how to use the control programaticallymemberViktar Karpach15:00 7 Sep '08  
QuestionRe: problem resolved: how to use the control programaticallymemberGoslow5:55 13 Oct '08  
AnswerRe: problem resolved: how to use the control programaticallymemberViktar Karpach14:22 13 Oct '08  
GeneralRe: problem resolved: how to use the control programaticallymemberGoslow22:02 13 Oct '08  
Generalproblem in get and set value [modified]memberharish4486:02 5 Sep '08  
GeneralRe: problem in get and set valuememberpixel3cs1:52 7 Sep '08  
GeneralRe: problem in get and set valuememberViktar Karpach15:02 7 Sep '08  
GeneralRe: problem in get and set valuememberharish4485:58 9 Sep '08  
GeneralAJAX Toolkit Update PanelmemberThePervo6:17 27 Aug '08  
GeneralRe: AJAX Toolkit Update PanelmemberThePervo6:57 27 Aug '08  
GeneralRe: AJAX Toolkit Update PanelmemberViktar Karpach18:03 7 Sep '08  
Generalselected valuememberharish44822:26 25 Aug '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 20 Jul 2008
Editor: Smitha Vijayan
Copyright 2008 by Viktar Karpach
Everything else Copyright © CodeProject, 1999-2008
Web18 | Advertise on the Code Project