Dynamically loading WebUserControl
Hi there,
im having an issue dynamically loading a webusercontrol.
In my vs2008 solution i have several projects. their is the main web project, then several other projects that are controls to be used in the main project.
In the main web application when a user requests a page it will dynamically load a WebUserControl in on of these other projects, based on a supplied Id in the querystring.
I publish each project that has a webusercontrol to a directory called controls under the main project.
ie:
c:\inetpub\wwwroot\MyWebProject
c:\inetpub\wwwroot\MyWebProject\Controls
so when i try and load a web control to my default aspx page i do the following
Dim defaultCtrl As Control = LoadControl("~/Controls/DefaultWebControl/newPage.ascx")
me.Controls.Add(defaultCtrl)
however i keep gettin an exception saying "Could not load type XXXXXX"
and the exception is of httpparser.
this is very frustrating, i cant see why this doesnt load.
if i try load the webusercontrol into a aspx page in the same project it will load. But it will not load for my main project..
Thanks for any help
Re: Dynamically loading WebUserControl
did you import the control into the other projects?
Re: Dynamically loading WebUserControl
the exeption occurs on the LoadControl line.
my web control is going to have all my html, im loading the control into a empty aspx.
cheers
Re: Dynamically loading WebUserControl
yeah I just re-read and edited my original post... it's too early here. :P
Re: Dynamically loading WebUserControl
haha no worries....
what do u mean by import? How do i import the control into my main project?
Re: Dynamically loading WebUserControl
Well i found the issues was the i need to add a reference to the main project to the project that contains the web controls.
I was hoping that i wouldnt need to this as i this means i will have to rebuild and publish the main project anytime i need to add a new control...
does anyone know of anyway around this...
Re: Dynamically loading WebUserControl
I don't think... there is a way around the build problems and potential unforseen runtime bugs. I have often wanted to break apart a large website into different projects but it's just been too problematic.
Re: Dynamically loading WebUserControl
Of course the ASCX needs to be in the same project as the web application. The entire context of an ASCX is that it is a web user control for the project in which it exists. If the control sits outside the web project, it needs to be a server control, because the only reason you're make it an external web user control is if it was meant to be reused across web projects.
Is there a way around it? Yes, in crude but epic proportions. It'd involve you using reflection. Or, you could just have a look at DotNetNuke's code to see how they do it. People write frameworks for this kind of stuff :D