简体   繁体   中英

Loading web user controls from blob storage in asp.net

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 ?

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM