Search code examples
c#asp.netascx

How to display a user control source code on an .aspx page?


I am trying to build an aspx page that displays a user control along with the code and code behind for the user control. Is there a way to get the code from a user control page and display it?


Solution

  • You could do this:

    FileInfo myControl = new FileInfo(Server.MapPath(@"~\test.aspx.cs"));
    StreamReader myControlSource = myControl.OpenText();
    string myControlSourceHtml = Server.HtmlEncode(myControlSource.ReadToEnd());
    

    Put that in a method and then you could point it to any file to get the source code as HTML and then use CSS to style as you see fit.