Uncategorized

ASP.Net dynamic code generation

August 16, 2005

author:

ASP.Net dynamic code generation

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

 

Founder NftyDreams; founder Decentology; co-founder DNN Software; educator; Open Source proponent; Microsoft MVP; tech geek; creative thinker; husband; dad. Personal blog: http://www.kalyani.com. Twitter: @techbubble
Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.