In the beginning in November, I was in Barcelona. Apart from the fact that the Microsoft TechEd took place there, Barcelona is obviously in itself a landmark. One of my favorite places in the metropolis is the Sagrada Familia, the famous temple by Antoni Gaudi. Frequently used as a metaphor for colossal and never ending type of software development projects especially government bodies and financial institutions seem to have patented. However, inside the dark bowls of the Sagrada other, much more interesting processes, take place.

Apart from the complete models in the basement of the template, on the ground floor hard workers you might encounter small teams of construction workers, complete with helmets and fags, concentrating on miniature activities: creating mosaics of tiny tiles. The first highly focussed construction worker signs the tile, the second one cuts it and passes it back to the first who then tries to fit it in the mosaic on the drawing board. Wonderful. I suspect that after a long day of work both construction workers happily glazing enjoy the subway ride home. Fine work, even without any view what so ever on the overall progress of the project. There’s no better analogy for developers who try to find little solutions for miniature problems in everyday software development. Identifiable right?

Earlier this week I wanted to create a number of link buttons dynamically on an ASP.NET web page, and then allow the Click event of these buttons to be handled by that page. Although that seems rather obvious, it requires quite a lot of reflection. A little mosaic.

Step one is a piece of cake. First create the button you want. Preferably do this from the OnInit event as it requires re-creation on post back!

LinkButton button = new LinkButton();

Now you will need to track down the EventInfo for the Click event. You need this to add the delegate handling the event. So

Type buttontype = button.GetType();
EventInfo clickeventinfo = buttontype.GetEvent("Click");

The most difficult step in this mosaic is dynamically creating the delegate. The easy way to do this is based on the name of the method on the webpage that should handle the event; which I’ve called ButtonClick. Original name isn’t it? My creativity sometimes abandones me. In addition, you must know what type of delegate should be created.

Type delegatetype = clickeventinfo.EventHandlerType;
Delegate delegateinstance = Delegate.CreateDelegate(delegatetype, ph.Page, handlername);

Another tile further. I have a delegate. Now I will have to pass the delegate to the event, so that, when I click the button, the method on the page gets triggered. This is a tricky one. Fortunately, the EventInfo class contains a method to take care of adding a delegate to the Click event. So, next I extract the MethodInfo for this particular method execute it through Invoke(). Additionally, I add the delegate as a parameter to this method.

MethodInfo mi = clickeventinfo.GetAddMethod();
Object[] args = { delegateinstance };

mi.Invoke(button, args);

Now my little mosaic is almost done. Now I only need to add the button to the list of controls on the web page (or in my particular case to the list of controls of the placeholder ph on the user control on the web page) and I’m done.


I feel like a happy construction worker at the Sagrada Familia site. Tired but satisfied. I’ve added a beautiful little mosaic as a tiny component of the application I’m working on. Writing code is still the best job in the world. With a serene smile I get into my car and get stuck in traffic.