|
|||||||||||||||||||||
|
|||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionThere are plenty of JavaScript color picker controls available out there, but none them are easy to distribute and fully designer supported. Moreover, integrating a JavaScript color picker to an ASP.NET app is a really awful experience. For this reason, I really felt a need for a color picker control in my toolbox that is easy to use but yet powerful. My idea of a color picker was to give the user a bundled control which he/she can easily drag and drop from the toolbox without writing a single line of code. Also, the control should be such that it doesn't postback every time a color is chosen. This article is not all about a color picker, but I have tried to give an overview of creating and deploying a custom control in ASP.NET 2.0. RequirementsNothing is required in using this control, except you must have one of the Visual Studio 2005 versions that are available out there. Also, I have packaged the control as a VSI file. Therefore, if you have a management studio express installed in your PC, then the VSI package may not install properly. The reason is that it overrides some registry settings. So, you might need to go through the following steps:
Or, you can just reference the colorpickerLib.dll from the "Add a toolbox item" option in the Tools menu of Visual Studio, to include the control in your toolbox. Behind the ControlThe first thing when building the control is to construct the color matrix, which is pretty simple. Create three variables int onrow = 1;
int oncol = 1;
int count = 1;
string s = string.Empty;
string set1 = string.Empty;
string set2 = string.Empty;
for (int r = 5; r >= 0; r--)
{
for (int g = 5; g >= 0; g--)
{
for (int b = 5; b >= 0; b--)
{
if (oncol == 1) s += "<tr>\n";
s += "\t<td onmouseover=\"colorOver(this);\" " +
"onclick=\"colorClick(this);\" style=\"cursor:pointer;" +
"background:#" + ReturnHex(r) + ReturnHex(g) +
ReturnHex(b) + ";\" class=cc></td>\n";
oncol++;
if (oncol >= 19)
{
s += "</tr>\n";
oncol = 1;
if (onrow % 2 == 0)
{
set2 += s;
}
else
{
set1 += s;
}
s = "";
onrow++;
}
count++;
}
}
}
Now, the challenging part. I have to render the matrix in a floating For embedding the resource and for getting the reference of the JS file, I had to do the following:
Now, ASP.NET has a cool function called ClientScriptManager cs = Page.ClientScript;
cs.RegisterClientScriptInclude("popscript",
cs.GetWebResourceUrl(this.GetType(),
"ColorPickerLib.default.js"));
So, I have created the color matrix, and created the JS script to render it off. Now, I have to wrap in the stuff which includes creating an image button, floating the matrix So, first, we have to get the contents from the matrix rendering control, which is in ColorPickerPalate.cs. In each Web control, there is method called StringWriter builder = new StringWriter();
Html32TextWriter writer = new Html32TextWriter(builder);
ColorPickerPalate details = new ColorPickerPalate(this);
details.RenderControl(writer);
builder.Close();
string popup = builder.ToString();
popup = popup.Replace("\r", string.Empty);
popup = popup.Replace("\n", string.Empty);
popup = popup.Replace("\t", string.Empty);
popup = HttpUtility.HtmlEncode(popup);
Here, you must have noticed that I encoded the HTML output. This is to avoid JavaScript errors (I get a JavaScript error on calling the // Inside showDiv Function (Part of the code)
_Source = docSource;
document.getElementById(element).innerHTML = divText;
displayFloatingDiv(element, "Color Picker", 300, 200, x, y);
Inside the originalDivHTML = document.getElementById(divId).innerHTML;
originalDivHTML = decode(originalDivHTML);
document.getElementById(divId).innerHTML =
addHeader + originalDivHTML; //+ originalDivHTML;
Finally, I have to link the Inside the if (Root.ID == Id)
return Root;
foreach (Control Ctl in Root.Controls)
{
Control FoundCtl = FindControlRecursive(Ctl, Id);
if (FoundCtl != null)
return FoundCtl;
}
return null;
And inside the if (!base.DesignMode)
{
try
{
TextBox controlToFind = null;
if (this.Page.Master != null)
{
controlToFind = FindControlRecursive(this.Page.Master,
_Control.ID) as TextBox;
}
else
{
controlToFind = FindControlRecursive(this.Page,
_Control.ID) as TextBox;
}
docSource = controlToFind.UniqueID;
string popup = CreateColorTemlate();
_Link.Attributes.Add("onclick", "showDiv( 'windowContent','" +
docSource + "', \"" + popup + "\")");
}
catch
{
throw new Exception("ControlToSet Property can not be left blank");
}
Also, I have to traverse separately the Deploying the ControlIn ASP.NET 2.0, you can distribute your custom controls easily, by creating a VSI file. The process is pretty simple. Just create a folder, put all the contents inside, and create a manifest that holds the information of the contents. Finally, zip the folder, and change the file extension from ZIP to VSI. That's it. Inside the manifest, in the case of a custom control, I have to write the following information: <?xml version="1.0" encoding="utf-8" ?>
<VSContent xmlns="http://schemas.microsoft.com/developer/vscontent/2005">
<Content>
<FileName>ColorPickerLib.dll</FileName>
<DisplayName>Color Picker</DisplayName>
<Description>Postback less easy to use color picker</Description>
<FileContentType>Toolbox Control</FileContentType>
<ContentVersion>2.0</ContentVersion>
</Content>
</VSContent>
UsageDrag and drop the control from the toolbox to the page. From the list of textboxes, choose the appropriate one in the Revision
| ||||||||||||||||||||