Rendered useless

To render, or not to render. So for some reason i have a stigma against having HTML in my c# code, i still do it all the time, but i get a bit ashamed and hope no one is looking over my shoulder. It stinks because to make changes the application will have to be recompiled , but since the web is stateless it's not a huge issue to replace the DLL every now and then, it's still clunky. There is the alternative of putting the HTML in a Database.

Note for a large section of HTML I always put it in a file and simply read it from file.

There is another alternative for HTML, using the .net HTMLControl.RenderControl, in which HTML is at least guaranteed to be valid and it looks better in the code. I tried it out and you have to do this to get the HTMLControl into text:

public static string RenderWebControl(HtmlControl html)
{
string htmlContent;

using (MemoryStream dataStream = new MemoryStream())
{
using (StreamWriter textWriter = new StreamWriter(dataStream, Encoding.UTF8))
{
using (HtmlTextWriter htmlWriter = new HtmlTextWriter(textWriter))
{
html.RenderControl(htmlWriter);
textWriter.Flush();
dataStream.Seek(0, SeekOrigin.Begin);

using (StreamReader dataReader = new StreamReader(dataStream))
{
htmlContent = dataReader.ReadToEnd();
}
}
}
}

return htmlContent;

}
Not terrible but still a quite a few loops to jump though, way more than using string builder to build a table.

So what about the database option, i guess it's ok, it's a performance hit compared to having it in the code, but it allows for changes to be made to a live system, i guess it depends on how many string your going to need and what the focus of the system is. But after trying to render the controls i feel a bit better about poisoning my code with some foreign matter.


Model for sending XML to jquery in C#

.net web services can return structs of any simple datatypes as nicely serialized XML and jquery can consume that like this

struct usefullStuff {
string[] commands;
string content;
}

$("commands string", returnedXML).each(function() { eval($(this).text()); });
$("content", returnedXML).each(function() { $("body").append($(this).text()) ; });