8 Nov
2007

Flexible ASP.NET User Controls

Category:UncategorizedTag: :

In the application that I am currently working on there are several very similar screens. The obvious solution was to create a User Control (UC) that encapsulated the appropriate functionality. Apart from some labels, URLs etc. that were easily taken care of with some simple properties on the UC the only difference between the various screens was a call to the repository to obtain the necessary data (no MVP or service layer in this app).

The DTOs returned by the repository were all of the same type (after some refactoring). Using generics and NHibernate I was able to create a single method in the repository that returned the correct data. However I still had to pass the correct type into the repository.

Outside of the view layer I would have used generics but after some extensive Googling I could not find anyone who had successfully used this technique with UCs (if someone knows otherwise then please let me know). I was tempted to use reflection but my dislike of runtime (as opposed to compile time) errors stopped me

I ended up defining a delegate on the UC and passing the appropriate code into the UC from the Page. It works but I didn’t really like the solution. I’m sure that some smarty pants out there has a better idea and I can’t wait for them to tell me what it is.