Tuesday, June 07, 2005

LoadControl and HttpHandler

Yesterday, I found another interesting, shall we say "feature", of the .NET framework. I had a page class that derived from System.Web.UI.Page. In this page, I loaded a custom control. As a result of this custom control loading, a call is made to Page.LoadControl to load a user control into the page. All of this worked fine, until I decided that wanted to define an httpHandler in my web.config to load my custom page class. When I did that, my call to LoadControl would fail, and would inform me that I was trying to load a control from another application.

My LoadControl looked something like:


Thanks to Thomas Zumbrunn over at CodeProject, I found an interesting workaround that allows you to load controls from "other applications". In his case, he really did want to load controls in other web applications. I simply wanted to load controls that resided in a folder in my current application, but I wanted to do so when the page was loaded via the httpHandler directive in web.config.

What I ended up with looked something like:


Subtle, but it works. And it works both ways. I can load my page by creating an ASPX page instance that derives from my custom page class, or I can load my custom page class using the httpHandler directive in web.config.

Personally, I think that this is a bug in the .NET framework, but one that I doubt Microsoft will fix. I just wish they would at least document it in the documentation for LoadControl. C'mon Microsoft, get with the program!

No comments: