|

Introduction
In my previous articles, I've delved into the basics of using the XMLHttpRequest object to create, send, receive, and process XML requests asynchronously from within a .NET application. Now that I've illustrated the majority of the mechanics behind creating the objects from within JavaScript, I'm going to (hopefully) simplify matters by introducing you to what I believe is one of the cleanest and simplest AJAX toolkits for Visual Studio, and show you a quick and simple way to create an AJAX-powered color picker for your ASP.NET application.
Background
This example uses an AJAX toolkit referred to as Anthem. You may have seen the name thrown around quite a bit (especially in a lot of CodeProject articles). Anthem is a set of tools for Visual Studio that greatly eases the transition of your ASP.NET application into the world of AJAX. With some knowledge of Visual Studio, and virtually no knowledge of JavaScript, you can implement the functionality of AJAX controls simply by dragging the requisite control onto your web page and managing the code-behind.
I'm going to go into some detail on how to utilize the Anthem tools below, but before we begin, you should be sure you have the latest version of Anthem for your Visual Studio environment. You can download the control package (for free) at SourceForge. Anthem was created originally by Jason Diamond, and is now an active project on SourceForge. It is brilliant in its simplicity, and is definitely designed with the developer in mind. No, I'm not a shill for them. It's just good stuff.
Getting Started
Starting from this point, I'm going to assume a couple of things:
- You have downloaded Anthem
- You have built the project for your version of VS.NET
- You have added the controls to your VS.NET toolbox
After you have added the controls to your VS.NET toolbox, it should look something like the following:

In future articles, I will explore the utilization of some of the other controls provided, but for now, we're just going to stick with the Button and the Label controls.
Overview
Anthem's tools handle all of the JavaScript and XMLHttpRequests for you on the back end. You drag a control to a web page just like you would with an ASP.NET control. The main difference is that when you double click the control to add the default event handler, the Anthem controls create a callback function, instead of a postback function. This is the key to AJAX, and the beauty of the control set is that the event handler will look no different than a standard C# function in the code-behind.
In my example, I have created a simple HTML table that contains 48 cells (8 x 6). Within each cell is an Anthem button that has a certain background color. Each of these buttons has the same callback function within the code, as outlined below: private void palette_Click(object sender, System.EventArgs e)
{
Button btn = (Button)sender;
this.btnColor.BackColor = btn.BackColor;
this.btnColor.UpdateAfterCallBack=true;
this.lblColor.Text = ColorToHex(btnColor.BackColor);
this.lblColor.UpdateAfterCallBack=true;
}
The first part of the code implicitly casts the sender object into a Button type, because we know that the sender is in fact a Button. Now that we have the Button object (btn), we are going to set the btnColor object (another Button, this is the one that sits above the Label) to the background color of the sender object. Now, you will notice something important that is specific to Anthem: this.btnColor.UpdateAfterCallBack=true;
What we are doing is letting the function know that we have changed something about the btnColor object, and that after the callback is complete, it will need to be "updated". This is the piece of code that will ensure that the new background color of btnColor will actually change and reflect on the client side.
The next step is to update the lblColor (Label) with our hexadecimal color value. You will notice that it calls the ColorToHex(System.Color) function. This function (and associated helper functions) are provided by Sandro Todesco, since, to my knowledge, there exists no single method in .NET to convert a System.Color to its hexadecimal value. This does the dirty work for us, consuming a System.Color, and returning the hexadecimal value. The code is documented internally regarding the origin of the conversion code and where you can download an example. In the palette_Click() function, again, you will notice: this.lblColor.UpdateAfterCallBack=true;
Now that the Label has the hex value for the color, we'll make sure that the Label updates its text after the callback.
Using the Code
I have provided this demonstration of the functionality of Anthem for a couple of reasons. One, you may just want a color picker for your application. But, hopefully, you will also get a better feel for how easy Anthem is to utilize within your ASP.NET application. When you run the example, you might find it interesting to view the HTML source of the web page (right click on the page, then "View Source"). You will see all of the JavaScript functions that have been automatically created at runtime by the Anthem controls. Using AJAX in this manner enables people who are not JavaScript gurus to create very complicated AJAX-powered websites using the .NET language they are most comfortable with.
Happy coding!
| You must Sign In to use this message board. |
|
| | Msgs 1 to 25 of 25 (Total in Forum: 25) (Refresh) | FirstPrevNext |
|
 |
|
|
 |
|
|
Hi,
Great control works great with ie6, however don't share the same success when use ie7 and Opera 9.
Any pointers on getting it working with ie7 and opera?
Robin
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
I am somewhat new to .NET Ajax wrappers.
Could you please clarify the difference between using Atlas vs. Anthem?
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Sure!
Anthem is a set of (free) ASP.NET controls that are "third party", in that they were created by an entity separate from Microsoft. Anthem controls were created by Jason Diamond, and you can find more information about what they do, and who he is at his website: http://www.anthemdotnet.com/[^].
ATLAS is the name for the set of AJAX controls that are currently in CTP (community technology preview, aka "public beta") from Microsoft. They are designed for VS.NET 2005 and ASP.NET 2.0.
Each control set basically provides similar functionality to your Visual Studio environment. So, for example, instead of an ASP:Button control, you would have an Anthem:Button control. Instead of a "postback" function (from the ASP control), you would have a "callback" function that handles your XmlHttpRequest. Usage of these controls is very, very similar to how you use ordinary ASP controls. The beauty is the controls, though, is that you don't have to actually write any of the Javascript, as that is handled by the control set.
Hope that answers your questions, to some extent. If you have anything specific, please feel free to ask away.
Quae narravi nullo modo negabo.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
 |
|
|
Thanks man - I really look forward to your articles. You've helped me a lot. Please keep 'em coming
Tom Hamilton
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
I'm glad to be of help, and thanks for the comment! I'm also open to suggestions about future AJAX articles that may be of use, so, if you'd like to see something, please feel free to let me know.
Quae narravi nullo modo negabo.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
|
Hey, that's not true either. It returns the name if possible, if not possible, it returns the hexcode. I can prove it with some decompiled code if you want to
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Sure. If you can directly disprove an MSDN article about what the method returns I'd be more than interested to see it. 
However, though, from observed testing in a WinForms application moments ago using the system colors Red, Green, Blue, WhiteSmoke, AliceBlue, Gainsboro, Turquoise, and YellowGreen, it returns a string representation, and not a hexadecimal representation. This doesn't accomplish what I set out to do, which was convert a System.Color to a hexadecimal representation.
But, like I said, if you want to post some code to disprove the statement made in the MSDN article, I'd be more than happy to take a look.
Quae narravi nullo modo negabo.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Ok The code returns the name of the color _if_ possible, otherwise the hex-number. If you really, really for some unknown reason need to have the hex-number-style to make the code harder to follow you can allways: Color.FromArgb(my_color.ToArgb()) before you call the ToHtml-method 
public static string ToHtml(Color c) { string text1 = string.Empty; if (!c.IsEmpty) { if (!c.IsSystemColor) { if (c.IsNamedColor) //Is a named color? { if (c == Color.LightGray) //Some specialcase { return "LightGrey"; } return c.Name; //Return name in case the color has a name } return ("#" + c.R.ToString("X2", null) + c.G.ToString("X2", null) + c.B.ToString("X2", null)); //Return hex otherwise } switch (c.ToKnownColor()) //Switch-case for systemcolors, colors are only systemcolors if the really are, not if they happen to { case KnownColor.ActiveBorder: return "activeborder";
and so on...
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Fair enough. Readers, feel free to use the method of your choice.
Quote: If you really, really for some unknown reason need to have the hex-number-style to make the code harder to follow
Well, that's not really the point. This is an intermediate article, and the code is easy to follow and well commented. I don't generally consider simple string manipulation advanced or hard to understand. If you want to use named colors in your HTML, feel free. If you like more control and a wider support of colors, use Hex. It's a matter of preference to the designer. The code snippet I've included reliably returns hex values in all cases.
Quae narravi nullo modo negabo.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
> Well, that's not really the point. This is an intermediate article, and the code is easy to follow and well commented. I don't generally consider simple string manipulation advanced or hard to understand. If you want to use named colors in your HTML, feel free. If you like more control and a wider support of colors, use Hex. It's a matter of preference to the designer. The code snippet I've included reliably returns hex values in all cases.
I can't see any reason to why you want to add extra code for something that's built in into the framework, with even better support since it returns the name if possible. Why reinvent a half wheel when there is a whole and tested wheel ready to use? You won't get fewer colors just since you use named colors, but the html that's produced it's easier to read, and the code will return hex if needed. But just if needed.
Right now I'm getting a feeling that you think of the web as a windows application rather then html, and want's to transform html into something that html isn't :/ It's _better_ to use names. The only reason I can think of is if you want to access the colors from javascript on the client and modify them, but I've got a feeling that it's not the case this time...
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
This comment thread has far exceeded the intended scope of this application. Bait me all you like with thinly veiled insults about what I think the web is, but unless you have an actual question regarding the application, I will not be responding to this comment line any further. If you'd like to demonstrate to the readers why using named colors is "better" than using hex, I'd suggest you write an article.
Quae narravi nullo modo negabo.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
Usual, I could probably come up with a dozen reasons to use hex, but here are half a dozen off the cuff-- 1) I can paste them into the color picker dialog in Photoshop (if I want to match colors). This applies to other programs as well. 2) If I'm serializing the color in a text file or string (I know, I know, .Net already does serialization) I can predictably read rgb values to and from a 6 character representation, 3)I can paste the values directly into css or html, 4) I can use the values (with some minor alteration) in vb6, c++, or <insert language here>, 5) I can see from the value how it compares to another color (ie "hey, the red value is higher in this color")...yes, I do that. 6) It's very easy to extract the red, green, or blue component of a hex representation. You can do this in virtually any programming language, not just .Net.
All this boils down to: The hex representation is a universal format which represents the actual RGB values. The RGB values themselves represent real light beaming from your cathode ray tube or flatscreen into your eye. "Whitesmoke" is just a convention and useless for all of the above mentioned except for #3.
As for #2 (writing your own serialization)-- If you can't see why someone might want to write code to always use hex instead of taking Microsoft's wholesale, then it will probably be even harder to convince you why you might do your own serialization, but believe me, there are good reasons. (check this[^] out)
Just because you can't think of a good reason for something doesn't mean there isn't a good reason for doing it.
Don Jewett, MCSD
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
I cant think of any reason to why this application might need all the features of using plain hex you mention. Instead, there is more unimportant code in this example...
Anyway; 1) Pick the color directly from the webpage 2) I would serialize a color that way, it is unpractical 3) ctrl+c, ctrl+v 4) Fair use 5) Maybe fair use, if you do it manually at least. It not easier to convert hex to a color then a name in .net 6) Samt as 4)
More universal would be an integer, eaiser to parse...
And yes, I do write my own serialization sometimes...
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
You're preaching to the choir, Don. You don't need to convince me, though they're all valid points you made. The guy who continues to argue that conversion is a waste of code will never be convinced, though, since he just wants to argue and be antagonistic. Personally, I'll be sticking with hex, since my PhotoShop has no idea what AliceBlue is when I put it into the color picker. 
Thanks for the comment, though!
Quae narravi nullo modo negabo.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
oops-- sorry about that, usual. i meant all that for onkleborg.
thanks for your code and keep up the good work!
Don Jewett, MCSD
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
There is actually a very simple way (at least in vb), little example:
Dim currCol As new Color(whatever) dim webCol as string = "#" + Hex(currCol.R).toString() + Hex(currCol.G).toString() + Hex(currCol.B).toString()
works for me...
Chris.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
General News Question Answer Joke Rant Admin
|