Thursday, March 22, 2007

Beauty Of .NET (Extender Controls)

Have you ever wanted to extend functionality of .NET controls ? if so first thing you will do is subclasing the control and implement your functionality. Hmm there is a much more elegant way of doing this by using Extender Controls.

Extender controls allows you to extend functionality of exiting controls in a form without subclasing. You have to only drag the extender control to the form and there you go ..... all the controls in the form are equipped with the new functionality.

The basics are very simple, and you have to build the rest according to your needs. End of this article I have presented some interesting things that can be done using this technique. Extender control is a component so you have inherit your class from a component and implement the IExtenderProvider interface.

[ProvideProperty("HelloWorld",typeof(System.ComponentModel.Component))]
public class HelloWorldComponent : Component,IExtenderProvider

IExtenderProvider interface is very simple. You have to only implement the CanExtend method.

public bool CanExtend(object extendee)
{
if (extendee is Control && !(extendee is HelloWorldComponent))
{
return true;
}
else
{
return false;
}
}

The above will extend all the controls in a form (win or web) with properties extended from extender control. You can introduce one to many properties to controls in a form using one extender control. For introduce a property you have implement two methods Set(Component,String) and Get(Component). ProvideProperty attribute gives names of the properties. Pls refer Extender Controls Tutorial for more details on creating and using extender controls.


Interesting things you can do with Extender Controls

1) UI Security Implementations
2) Two Way Data Binding in ASP.NET for simple controls like text boxes and radio buttons

Have fun.........

No comments: