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 ?
Asked
Active
Viewed 866 times
0
-
what did you try? how are you loading those controls back to the memory? in which format? – Davide Piras Sep 24 '11 at 09:52
1 Answers
0
I'd used following approach to load .ascx controls. In fact I've saved .ascx files into "text" field.
First of I've register a control in web.config file:
<pages>
<controls>
<add tagPrefix="tab" src="~/pg.ascx" tagName="Test"/>
</controls>
</pages>
In webform, I read .ascx content from the database and save it to pg.ascx under the root and load user control dynamically.
Control t = LoadControl("~/pg.ascx");
PlaceHolder1.Controls.Add(t);
EDIT: I'm adding a user control definition which I've stored into StringBuilder object.
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("<%@ Control Language=\"C#\" ClassName=\"m1\" %>");
sb.Append("<script runat=\"server\">");
sb.Append("int no=10;");
sb.Append("public void page_load(){ for(int i=1;i<=no;i++) { Response.Write(i);} }");
sb.Append("</script>");
sb.Append("<h1>Hello</h1>");
System.IO.File.WriteAllText(MapPath("~/pg.ascx"), sb.ToString());
Control t = LoadControl("~/pg.ascx");
PlaceHolder1.Controls.Add(t);

KV Prajapati
- 93,659
- 19
- 148
- 186
-
here "~/pg.ascx" is virtual path right ? , but i store my user control in some other location (blob storage) not in the website's folder – Kuntady Nithesh Sep 24 '11 at 10:06
-
Yes it is virtual file and ASP.NET wont complaint about that file (.ascx) - whether it is exists or not. – KV Prajapati Sep 24 '11 at 10:10
-
But my .ascx file is not under root . its outside the project folder – Kuntady Nithesh Sep 24 '11 at 10:14
-
It doesn't matter where that file is stored but the fact is you must have to copy it or create it (web user control) into current application. – KV Prajapati Sep 24 '11 at 10:18
-
Cant i bundle my user control in a dll and load from dat . I use this technique for by BO classes . Since they are inside a class library , I load them from assembly which is created using activator .createinstance . Cant i do something like that ? I couldnt do so since this is an ui file which is inside a web application . Is there any way to achieve in that manner ? – Kuntady Nithesh Sep 27 '11 at 06:32