[New Article] Building a Volta Control : A Flickr Widget
This is my first article which is based on the first CTP of Volta considering its current limitations. You will see how you can create a Volta control that the compiler can convert into an AJAX Widget without requiring us writing a single line of JavaScript code: http://dotnetslackers.com/articles/aspnet/BuildingAVoltaControlAFlickrWidget.aspx
Make HTML controls discoverable in Volta Control
When a Volta control is rendered, the ID attribute of the generated HTML is changed to something like _vcId_1_DivName which is inconvenient to find from code. But the ID attribute stays the same in case of Volta Page, so it is discoverable by ID like this:
Div divContent = Document.GetById<Div>("divContent");
However, if you add HTML controls to the control like the following, the ID is not changed during the rendering:
public VoltaControl1() : base("VoltaControl1.html") { InitializeComponent(); Button btnClick = new Button(); btnClick.InnerText = "Click!"; btnClick.Id = "btnClick"; this.Add(btnClick); }
If you don’t prefer this way and seriously want to write your own HTML in the control’s html page, you might find the following snippet useful. But, remember in this case you will use name attribute of the html element instead of ID.
// Usage: var element = GetElementByName(Document.GetElementsByTagName("div"), "divWidget"); private HtmlElement GetElementByName(HtmlElementCollection elements, string name) { foreach (var element in elements) { DomAttribute nameAttribute = element.Attributes.GetNamedItem("name"); if (nameAttribute != null) if (nameAttribute.Value == name) return element; } return null; }
Making cross domain AJAX call using Volta
Making a cross domain AJAX call in Volta is piece of cake. Volta compiler generates necessary client codes to make it work. Here is a snippet that can make an AJAX call to some Url and fetch data:
public void DownloadPhotos() { IHttpRequest request = HttpRequestFactory.Create(); request.AsyncSend("POST", URL, string.Empty, delegate(string response) { OnPhotosLoaded(new PhotosLoadedEventArgs(response)); }); }
Both IHttpRequest and HttpRequestFactory classes can be found in the Microsoft.LiveLabs.Volta.MultiTier namespace. AsyncSend method performs the asynchronous call and calls back the delegate defined where OnPhotosLoaded event is fired to notify the subscriber of this event that the data has just arrived.
Namespace Alias Qualifier - to get rid of crazy coding
Let us say somebody in your company loves crazy coding and really do not bother about his/her codes affect others. (S)He put class name System and a constant Console and now wondering how come a simple Console.WriteLine does not compile:
class System { int Console = 10; static void Main(string[] args) { Console.WriteLine("Hello World!"); // Compile time error System.Console.WriteLine("Hello World!"); // Compile time error } }
Making use of global::System.Console must solve your problem:
class System { int Console = 10; static void Main(string[] args) { global::System.Console.WriteLine("Hello World!"); global::System.Console.WriteLine("Hello World!"); } }
However, ever thought of a scenario where there can be same class name under two different namespaces? Here comes the role of Namespace Alias Qualifier. In namespace declaration by "using", aliases can be assigned to namespaces so that they might be useful in later part of the code as shorthand and most importantly will solve the problem of ambiguity:
using sys = System; using mine = MyProject.Process; … … sys.Console.WriteLine(mine.Console["width"]);

