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 usercontr开发者_运维技巧ol to my web page ?
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);
精彩评论