C
Carl
Hi,
I am developing a web portal where “tenants†should be able to register their own Web user controls. Basically what I need to do is to load and render the Web controls during run time. The problem is that I can’t recompile my portal application each time a new component is added so adding references to the project and using the LoadControl method is not an option.
My current code looks something like this,
object obj = null;
Assembly a = null;
Type type = null;
string url = "Path to User Controll dllâ€
a = System.Reflection.Assembly.LoadFile(url,null); //Load assembly
type = a.GetType("myUserControls.theControlToLoad"); //Get type def
obj = Activator.CreateInstance (type); //Create obj
MethodInfo mi = type.GetMethod("InitializeAsUserControl");
System.Web.UI.Page myPage = this;
object[] myParametersArray = new object[1];
myParametersArray[0]= myPage;
mi.Invoke(obj,myParametersArray); //Call method
System.Web.UI.UserControl myControl = (System.Web.UI.UserControl) obj; //Type cast
this.PlaceHolder1.Controls.Add(myControl); //Add control to web page
The problems I am having is,
a) No UI components in the User control gets instantiated i.e. they are NULL.
(I have been able to work around this by creating a public method that instantiates all the objects but there must be another way.)
b) I have to override the render method in the User control. If I don’t no GUI is shown when I add the control. If possible I would like to leverage the “GUI information†captured in the .ascx file so that I don’t have to add a bunch of HTML code in my render method.
Any help would be greatly appreciated.
Thanks,
Carl
I am developing a web portal where “tenants†should be able to register their own Web user controls. Basically what I need to do is to load and render the Web controls during run time. The problem is that I can’t recompile my portal application each time a new component is added so adding references to the project and using the LoadControl method is not an option.
My current code looks something like this,
object obj = null;
Assembly a = null;
Type type = null;
string url = "Path to User Controll dllâ€
a = System.Reflection.Assembly.LoadFile(url,null); //Load assembly
type = a.GetType("myUserControls.theControlToLoad"); //Get type def
obj = Activator.CreateInstance (type); //Create obj
MethodInfo mi = type.GetMethod("InitializeAsUserControl");
System.Web.UI.Page myPage = this;
object[] myParametersArray = new object[1];
myParametersArray[0]= myPage;
mi.Invoke(obj,myParametersArray); //Call method
System.Web.UI.UserControl myControl = (System.Web.UI.UserControl) obj; //Type cast
this.PlaceHolder1.Controls.Add(myControl); //Add control to web page
The problems I am having is,
a) No UI components in the User control gets instantiated i.e. they are NULL.
(I have been able to work around this by creating a public method that instantiates all the objects but there must be another way.)
b) I have to override the render method in the User control. If I don’t no GUI is shown when I add the control. If possible I would like to leverage the “GUI information†captured in the .ascx file so that I don’t have to add a bunch of HTML code in my render method.
Any help would be greatly appreciated.
Thanks,
Carl