Asp – Dynamically adding user controls registered in web.config

asp.net

I'm working on a project that has all its user controls registered in its web.config file (which seems very clean and tidy)

So far so good (here comes the problem) however I'm trying to dynamically create and add user controls to a page. These user controls fire events that need handling.

Ordinarily that wouldn't be a problem:
You just register the control in the page, load the control, cast it to the correct type, and assign the event handlers, add it to the page, sit back and let the magic happen, easy peasy.

But I can't reference the control's type when the control is registered in the web.config, which means no cast, which means no event handling!

Weirdly you can reference the type if you add the usercontrol to the page at design time!

There must be a way round this (without having to register the control on the page, or add a control at design time), what on earth am I missing?

Best Answer

It's been a while, but I think I've seen this type of behavior in ASP.NET when a project is a Web Site and not the Web Application. As far as I remember, the Web Site compiles each page into its own assembly and with no common name space and regardless of config requires the <%@ Register %> directive. If you don't, you get the exact error of missing an assembly reference.

I would have to test to be sure...

Related Topic