Is there any way I can store the code of the UserControl in a database table, compile it dynamically or Load it from there ? Means I get a string from database that contains the complete code of UserControl and then add it to the page ?
A few ideas:
Option 1: temp files (easiest)
~/tmp/
(and give the web application modify & create permissions to that directory)Save the UserControl contents to a temp file:
string userControlContents = /* get user control contents from database */;
string path = Server.MapPath("~/tmp/2011081612332423.ascx");
System.IO.File.WriteAllText(path, userControlContents);
Load the user control:
Control c = UserControl.LoadControl("~/tmp/2011081612332423.ascx")
Add the user control to the desired page:
this.Controls.Add(c);
Option 2: HttpHandler
Response
stream
Then load the control as in Option 1:
Control c = UserControl.LoadControl("~/UserControlFromDB.ascx?id=392")
id=NNN
changes). URL rewriting might work around this.Option 3: Compiling pages instead of just controls:
BuildManager.CreateInstanceFromVirtualPath()
compiles a Page
in ASP.NET from a virtual path. So you could store an entire page in the database and compile it dynamically.
Disclaimer: I don't recommend storing controls or pages in the database; it will increase maintenance, debugging, and security costs.