Thoughts, Tips and Tricks on what I'm currently do for a living. Currently most of my spare time is spent on contributing to Akka.NET.

Monday, May 21, 2007

Insert Do-nothing comments

Always insert a //Do nothing comment in places where the intention is that nothing should be done. Let Me show you why by an example. Assume you have a constructor for a Control that inherits WebControl and all you want to do is changing the default tag (which is Span).
public MyControl()
   :base(HtmlTextWriterTag.Div)
{
}
When someone else reads this code (or you in a month or so when you've forgotten all about it) they might wonder if this code has been finished or if there is something missing in the constructor. By adding a comment, you show for anyone reading the code that there shouldn't really be any code inside the constructor.
public MyControl()
   :base(HtmlTextWriterTag.Div)
{
   //Do nothing
}
I have a code snippet for this so I just need to type don and hit tab twice and //Do nothing is inserted.

Friday, May 18, 2007

Passing values to behavior properties

Typically a property in a AjaxControlToolkit Extender looks like this:
[ExtenderControlProperty]
[DefaultValue("")]
public string MyProperty
{
  get { ... }
  set { ... }
}
Which will match a property in the behavior:
get_MyProperty : function() { ... },
set_MyProperty : function(value) { ... }
Values for MyProperty on the server side will end up in the behavior. Sometimes you want to pass values to the behavior without creating a property in the Extender control. To do this, simply override RenderScriptAttributes() and add the properties using AddProperty().
protected override void RenderScriptAttributes(ScriptBehaviorDescriptor descriptor)
{
  base.RenderScriptAttributes(descriptor);
  string horizontalAlignment=GetHorizontalAlignment();
  descriptor.AddProperty("HorizontalAlignment", horizontalAlignment);
}
And voilà, the HorizontalAlignment property in the beahvior will be set.

Property names & Extenders

Properties for extenders inheriting ExtenderControlBase typically looks like this:
[ExtenderControlProperty]
[DefaultValue("")]
public string MyProperty
{
  get
  {
    return GetPropertyValue("MyProperty", "");
  }
  set
  {
    SetPropertyValue("MyProperty", value);
  }
}
You must have a property with the same name in the behavior on the client side:
get_MyProperty : function() {
  return this._myProperty;
},
set_MyProperty : function(value) {
  this._myProperty = value;
}
But what if you want it to have different name on the client side? Easy. Add the ClientPropertyName attribute to the server side property. If we want MyProperty to be called myProp instead this is how it's done:
[ExtenderControlProperty]
[DefaultValue("")]
[ClientPropertyName("myProp")]
public string MyProperty
{
  get
  {
    return GetPropertyValue("MyProperty", "");
  }
  set
  {
    SetPropertyValue("MyProperty", value);
  }
}
On the client side we get:
get_myProp : function() {
  return this._myProperty;
},
set_myProp : function(value) {
  this._myProperty = value;
}
More on attributes: http://ajax.asp.net/ajaxtoolkit/Walkthrough/ExtenderClasses.aspx

Thursday, May 10, 2007

OOP in Javascript

MSDN Magazine May 2007 edition has an interesting article on how to use object oriented techniques in Javascript. A must-read if you are using Microsoft AJAX since the entire client script library is written using these techniques. http://msdn.microsoft.com/msdnmag/issues/07/05/JavaScript/default.aspx