In C # 3.0, are there any classes that help me generate static html?

I am developing an HTML form designer that should generate static HTML and display this to the user. I keep writing ugly code like this:

public string GetCheckboxHtml()
{
    return ("<input type="checkbox" name="somename" />");
}

Is there a set of strongly typed classes that describe html elements and allow me to write code like this instead:

var checkbox = new HtmlCheckbox(attributes);
return checkbox.Html();

I just can't imagine the right namespace to search for this or the right search term to use on Google.

+5
source share
5 answers

, ASP.NET MVC DLL ( ... )... HTML, .

+5

XElement . . .

Html :

var input = new XElement("input",
    new XAttribute("type", "checkbox"),
    new XAttribute("name", "somename"));

return input.ToString();
+2

System.Web.UI.HtmlControls. RenderControl html.

HtmlInputCheckBox box = new HtmlInputCheckBox();

StringBuilder sb = new StringBuilder();
using(StringWriter sw = new StringWriter(sb))
using(HtmlTextWriter htw = new HtmlTextWriter(sw))
{
    box.RenderControl(htw);
}
string html = sb.ToString();
+2

HtmlTextWriter , "WriteStartTag" "WriteEndTag", HTML.

HtmlTextWriter, HTML WriteEndTag.

You can also use pre-written HTMLControls that port this code to strongly typed classes.

0
source

Also consider using System.Xml. Using it, you almost guarantee that your HTML complies with XHTML.

0
source

All Articles