Created
October 25, 2013 20:59
-
-
Save orangutanboy/7161746 to your computer and use it in GitHub Desktop.
This is an implementation of a disposable HtmlHelper extension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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