Skip to content

Instantly share code, notes, and snippets.

@orangutanboy
Created October 25, 2013 20:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save orangutanboy/7161746 to your computer and use it in GitHub Desktop.
Save orangutanboy/7161746 to your computer and use it in GitHub Desktop.
This is an implementation of a disposable HtmlHelper extension
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Web.Mvc;
namespace Demo.Helpers
{
public static class TableExtensions
{
public static MultiColumnTableHelper BeginMultiColumnTable(this HtmlHelper htmlHelper, params string[] headers)
{
return new MultiColumnTableHelper(htmlHelper.ViewContext.Writer, headers);
}
public class MultiColumnTableHelper : IDisposable
{
private TextWriter _textWriter;
public MultiColumnTableHelper(TextWriter textWriter, IEnumerable<string> headers)
{
_textWriter = textWriter;
var htmlBuilder = new StringBuilder();
htmlBuilder.Append("<table><thead><tr>");
foreach (var header in headers)
{
htmlBuilder.AppendFormat("<th>{0}</th>", header);
}
htmlBuilder.Append(@"</tr></thead>");
_textWriter.Write(htmlBuilder.ToString());
}
public void Dispose()
{
_textWriter.Write("</table>");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment