Tuesday, August 16, 2005
by Nik Kalyani
Tuesday, August 16, 2005 4:53:06 PM (Pacific Standard Time, UTC-08:00)

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("<script runat=\"server\">"
);
tw.WriteLine(" protected void Page_Load(object s, EventArgs e)"
);
tw.WriteLine(" {"
);
tw.WriteLine(" Response.Write(\"Hello World!\");"
);
tw.WriteLine(" }"
);
tw.WriteLine("</script>"
);
tw.Flush();
tw.Close();
Control dynamicControl = Page.LoadControl(dynamicControlUrl);
Page.Controls.AddAt(0, dynamicControl);
dynamicControl = null
;
File.Delete(dynamicControlPath);

 

RSS feed
Search and Links
Bling

View Nik Kalyani's profile on LinkedIn

Contact me: nik*kalyani.com (replace "*")

TechBubble
www.flickr.com
This is a Flickr badge showing public photos from techbubble. Make your own badge here.
Statistics
Total Posts: 204
This Year: 22
This Month: 0
This Week: 0
Comments: 231
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008
Nik Kalyani
Sign In
All Content © 2008, Nik Kalyani