1

I closely followed the link C# - Correct Way to Load Assembly, Find Class and Call Run() Method and got the code to load classes from assembly dynamically.

             Assembly assembly = Assembly.LoadFile(assemblyName);
             Type type = assembly.GetType(className);
             object objCustomClass = Activator.CreateInstance(type) as object; 

where assemblyName & className are got from database

But i need to load usercontrol classes from database . I dont have assembly since user control is in UI itself . How can do this ?

Edit : I am changing my question a bit . Now i am storing user control file in some other location may be in blob storage or some other place. ( not in my project work space ) and i store the url of the user control in database . Now how can load this usercontrol to my web page ?

Can i create a dll out of all my user controls and load exactly as above code ?

Community
  • 1
  • 1
Kuntady Nithesh
  • 11,371
  • 20
  • 63
  • 86
  • When you say "user control is in UI itself", doesn't that just mean that it lives in the main assembly (your executing assembly)? In that case the assembly is already loaded. – Alxandr Aug 24 '11 at 09:37

3 Answers3

0

Yes, just pragmatically create an object of your UI control when required and use this.Controls.Add(cbName); method to do that.

Please look at this link for details

Prashant Lakhlani
  • 5,758
  • 5
  • 25
  • 39
  • I know that ,I think you didn't get my question. I dont know the class name itself to create the object , that should be loaded from database . – Kuntady Nithesh Aug 24 '11 at 09:44
  • In that case, you should use executing assembly, since all the user controls will be in your current assembly. http://msdn.microsoft.com/en-us/library/system.reflection.assembly.aspx. – Prashant Lakhlani Aug 24 '11 at 09:47
0

You can have either of the following two approaches

You can always make your user control a custom control. They you will have an assembly which you can store it in your DB.

Or else if you want to load usercontrols dynamically you can store the path of the usercontrol('\controls\usercontrol.ascx') on your DB and can load it dynamically just like any other control

Ashley John
  • 2,379
  • 2
  • 21
  • 36
0

You use MyControl control = (MyControl)Page.LoadControl("mycontrolname.ascx")

Xhalent
  • 3,914
  • 22
  • 21
  • here "mycontrolname.ascx" is virtual path right ? , but i store my user control in some other location not in the website's folder – Kuntady Nithesh Aug 24 '11 at 11:14
  • Then you need to implement a VirtualPathProvider http://support.microsoft.com/kb/910441/en-us?spid=8940&sid=global – Xhalent Aug 25 '11 at 08:22