Search code examples
asp.nettemplateswebformslegacy

Include ASP.NET pages from C#


In a typical web framework

func viewHomepage()
    response.write(template.render("a string variable", ["an", "array"]))

is a fairly standard way of calling a templating engine and writing the output out.

Obviously the situation is reversed in ASP.net, since the templating engine sits in front of the code.

I am dealing with a legacy application that can't be rewritten. It's basically a 50 line xxx.aspx with a corresponding 20,000 LOC xxx.aspx.cs. What I want to do is write new "views" as separate ASP.net forms and controls and then include them back into the xxx.aspx.cs.

Essentially instead of doing:

case "newfeature":
{
    Response.Write("<table>");
    ...
    Response.Write("</table>");
}
break;

I want to do

case "newfeature":
    Response.Write(THEFUNCTIONIMLOOKINGFOR("newfeature.aspx"));
break;

That way there'll be some notion of modularity and it won't be reminiscent of a perl CGI script.

Show me a path to sanity pretty-please.


Solution

  • This can be done quite simply by calling the RenderControl method. You just need to pass it an HtmlTextWriter instance.

    The technique is described here: 4GuysFromRolla.com "Emailing the Rendered Output of an ASP.NET Web Control"

    There's also the MSDN Reference for the RenderControl method.