Skip to content

Instantly share code, notes, and snippets.

@austegard
Created July 31, 2009 16:12
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 austegard/159291 to your computer and use it in GitHub Desktop.
Save austegard/159291 to your computer and use it in GitHub Desktop.
Extensions to the TagBuilder class
using System.Web.Routing;
using System.Web.Mvc;
namespace MvcExtensions
{
/// <summary>
/// TagBuilder Extension Methods meant to streamline its use.
/// </summary>
public static class TagBuilderExtensions
{
/// <summary>
/// Similarly to <see cref="M:TagBuilder.MergeAttributes"/> merges the attributes passed to it but
/// accepts the attributes in the form of an anonymous object, and also returns the <see cref="TagBuilder"/>
/// instance, allowing chaining.
/// </summary>
/// <param name="tb">The <see cref="TagBuilder"/> instance to extend.</param>
/// <param name="tagAttributes">The tag attributes.</param>
/// <returns>
/// The same <see cref="TagBuilder"/> instance
/// </returns>
public static TagBuilder WithAttributes(this TagBuilder tb, object tagAttributes)
{
tb.MergeAttributes(new RouteValueDictionary(tagAttributes));
return tb;
}
/// <summary>
/// Similarly to <see cref="M:TagBuilder.AddCssClass"/> adds inner Html but also returns the
/// <see cref="TagBuilder"/> instance, allowing chaining.
/// </summary>
/// <param name="tb">The <see cref="TagBuilder"/> instance to extend.</param>
/// <param name="cssClass">The CSS class.</param>
/// <returns>
/// The same <see cref="TagBuilder"/> instance
/// </returns>
public static TagBuilder WithCssClass(this TagBuilder tb, string cssClass)
{
tb.AddCssClass(cssClass);
return tb;
}
/// <summary>
/// Similarly to <see cref="M:TagBuilder.GenerateId"/> generates the Id of the tag, but also
/// returns the <see cref="TagBuilder"/> instance, allowing chaining.
/// </summary>
/// <param name="tb">The <see cref="TagBuilder"/> instance to extend.</param>
/// <param name="cssClass">The id name.</param>
/// <returns>
/// The same <see cref="TagBuilder"/> instance
/// </returns>
public static TagBuilder WithGeneratedId(this TagBuilder tb, string name)
{
tb.GenerateId(name);
return tb;
}
/// <summary>
/// Similarly to <see cref="P:TagBuilder.IdAttributeDotReplacement"/> sets the id attribute dot replacement of the tag,
/// but also returns the <see cref="TagBuilder"/> instance, allowing chaining.
/// </summary>
/// <param name="tb">The <see cref="TagBuilder"/> instance to extend.</param>
/// <param name="idAttributeDotReplacement">The id attribute dot replacement.</param>
/// <returns>
/// The same <see cref="TagBuilder"/> instance
/// </returns>
public static TagBuilder WithIdAttributeDotReplacement(this TagBuilder tb, string idAttributeDotReplacement)
{
tb.IdAttributeDotReplacement = idAttributeDotReplacement;
return tb;
}
/// <summary>
/// Similarly to <see cref="P:TagBuilder.InnerHtml"/> sets the inner Html of the tag, but also returns
/// the <see cref="TagBuilder"/> instance, allowing chaining.
/// </summary>
/// <param name="tb">The <see cref="TagBuilder"/> instance to extend.</param>
/// <param name="innerHtml">The inner HTML.</param>
/// <returns>
/// The same <see cref="TagBuilder"/> instance
/// </returns>
public static TagBuilder WithInnerHtml(this TagBuilder tb, string innerHtml)
{
tb.InnerHtml = innerHtml;
return tb;
}
/// <summary>
/// Similarly to <see cref="M:TagBuilder.SetInnerText"/> sets the inner text of the tag, but also returns
/// the <see cref="TagBuilder"/> instance, allowing chaining.
/// </summary>
/// <param name="tb">The <see cref="TagBuilder"/> instance to extend.</param>
/// <param name="innerHtml">The inner text.</param>
/// <returns>
/// The same <see cref="TagBuilder"/> instance
/// </returns>
public static TagBuilder WithInnerText(this TagBuilder tb, string innerText)
{
tb.SetInnerText(innerText);
return tb;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment