Building a Custom Ajax WebControl with Gaia
There are a couple of easy steps you should do when you create a
Custom Ajax WebControl in Gaia, this tutorial tries to lead you through those steps so that the problems you encounter will be as small as possible.
The basic building blocks
The first thing you must do is to
create a new project. Or to be more specific a Visual Studio 2005
class library! You can use any language you wish but this example utilizes the marvels of Anders Hejlsberg... (C# that is ;)
When you've done so you must add references to the
System.Web assembly in addition to the WebWidgets assembly. (the Gaia dll)
Then there's a couple of
boiler plate files you need. You'll need a JavaScript file to host the JavaScript of your Gaia WebControl, you'll need a class file to hold the inherited WebControl class you create and possibly other files to assist you in your WebControl creation. We're gonna use the
Gaia Ajax Custom Ajax Control Widget example in this tutorial and therefore we need nothing more than one JavaScript file and one C# file to hold our
WebControl class. These files are called
Dice.cs and
Dice.js One
very important step is that you right click the Dice.js file and choose
Properties and set its
Build Action to
Embedded Resource. If you fail to do this your JavaScript file will never be able to load in the browser.
Getting dirty
Now that you've got the basic skeleton finished we can start working on the stuff that matters! :)
I am not going to show the complete contents of the class or the JavaScript file, mostly because you can view them if you
download Gaia, but I am gonna show you the most important things.
The
most important thing to remember is to add the
WebResource attribute somewhere inside any of your compilable C#/VB.NET files, you can add it anywhere you like, and it'll probably be nifty to have all of them at the same place like for instance inside the Assembly.cs file, but we just add it into our Dice.cs file like this:
This basically tells the compiler that our JavaScript file is a WebResource and should be served whenever we use stuff like the Manager.Instance.AddInclusionOfFileFromResource etc...
Without that line the consumers of your WebControl is forced to explicitly include your JavaScript file every time they consume your WebControl!
Note also the funny "filename" og the JavaScript file, this is the
complete namespace plus any
directories the file exists within! If you're having trouble downloading the JavaScript file or the JavaScript file comes down
empty I can almost guarantee you that you've messed this up. Remember to use the
default namespace of the Assembly plus all the
directories separated by a "." This is one of those places where Microsoft could definitely do some change of logic, I think this is the most bug-prone issue when you're creating WebControl no matter which framework you develop your controls in/for.
The WebControl class
The first line is basically just a helper for the IDE to tell it how to include that WebControl when it's dragged onto a WebPage. The second interesting thing here is that we're inheriting from the ASP.NET WebControl class. Gaia tries as much as possible to
not spend your single inheritance and force you to inherit your stuff from specific classes which obviously then destroys your possibility to inherit from your OWN classes. The third interesting thing here is the IUpdateableControl interface implementation.
As I said earlier if you want to view the
complete code you should
download gaia. I am just gonna walk you through some of the most important parts here.
Continue to the OnInit method