ASP.Net supports dynamic code generation and compilation. Sometimes, you just need a quick and dirty solution. This is one such solution. It writes out a control script, loads it and then deletes the file.

string dynamicControlUrl = “~/” + System.Guid.NewGuid().ToString() + “.ascx”;
string
dynamicControlPath = Server.MapPath(dynamicControlUrl);
TextWriter tw = new
StreamWriter(dynamicControlPath);
tw.WriteLine(“<%@ Control language=\"c#\" %>“
);
tw.WriteLine(
);
tw.Flush();
tw.Close();
Control dynamicControl = Page.LoadControl(dynamicControlUrl);
Page.Controls.AddAt(0, dynamicControl);
dynamicControl = null
;
File.Delete(dynamicControlPath);

 

Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

© 2012 TechBubble Suffusion theme by Sayontan Sinha