I have an ASMX service which serves some partial-HTML to a web-app as part of a JSON object. Until now, I've just been building the HTML in code, using StringBuilders. This is a huge pain since the formatting is really hard to read and I can't use any of Visual Studio's/Resharper's code completion, syntax highlighting, and other convenient features.
I tried to solve this with User Controls (I'm not committed to this approach if there is a better way. All I need are some very simple parametrized static partial HTML pages), but now I am running into problems when I try to render the control like this:
public override string Html
{
get
{
StringWriter writer = new StringWriter();
HtmlTextWriter htmlWriter = new HtmlTextWriter(writer);
CreateTestWizardPartials.TestPeriods testPeriodsHtml = new CreateTestWizardPartials.TestPeriods();
testPeriodsHtml.RenderControl(htmlWriter);
htmlWriter.Flush();
return writer.ToString();
}
}
This always returns an empty string. I've read you need to use Page.LoadControl() to dynamically load User Controls but there is no Page for me to use it with. Is there a workaround or a better solution than User Controls?
Thanks!
You could load static HTML file that contain the partial bits of markup you need. If you need to somehow bind the markup with dynamic data, then render the HTML files through a template engine.
Here's and older post regarding some template systems for ASP.Net: Can you recommend a .net template engine?
And another more recent post regarding ASP.Net MVC: JQuery's $ is in conflict with that of StringTemplate.Net in ASP.Net MVC