Contents tagged with Extension Methods

  • Extension Methods: HtmlTextWriter part 1 - RenderTag

    Posted Tuesday 17, August 2010, 8:51 in C#, ASP.NET, Extension Methods

    So, for me case is clear guys: “Static helper methods are dead, long live the Extension Methods.”
    Since extension methods were out, I become a big fan of them.
    For example, instead of:

    StringHelper.Encode(“Some secret”);

    it looks much more nice and natural to me:

    ”Some secret”.Encode();

    I have implemented and I’m using tons of extension methods so far and I decided to start a category in my blog where to share some of them with you guys.
    Hope all those methods to be useful to you too, as they are to me.

    First of the series will be a couple of extension methods to HtmlTextWriter.
    You know, as a web and especially web controls developer, I use it much and that’s why I like extending it.

    During my development I found a lot of lines where I just render and open tag and then immediately the closing (end) tag is rendered, like:

    writer.AddAttribute(HtmlTextWriterAttribute.Id, fieldName + "_Error");
    writer.AddAttribute(HtmlTextWriterAttribute.Class, "field-validation-error");
    writer.RenderBeginTag(HtmlTextWriterTag.Span);
    writer.RenderEndTag();

    The other case I often found myself using is: open tag, write some content text and closing the tag, like:

    writer.RenderBeginTag(HtmlTextWriterTag.Span);
    writer.Write("allow editing");
    writer.RenderEndTag();

    I have developed some snippets for Visual Studio, which you could find at my website, in order to speed up typing such a kind of code bocks, but at some point I said to myself: why not merge those lines in a single methods. Of course, in a single extension method smile_teeth.

    Here are the simple extension methods I came with:

    /// <summary>
    /// Renders the tag.
    /// </summary>
    /// <param name="writer">The writer.</param>
    /// <param name="tag">The tag.</param>
    public static void RenderTag(this HtmlTextWriter writer, HtmlTextWriterTag tag) {
        writer.RenderBeginTag(tag);
        writer.RenderEndTag();
    }
     
    /// <summary>
    /// Renders the tag.
    /// </summary>
    /// <param name="writer">The writer.</param>
    /// <param name="tag">The tag.</param>
    public static void RenderTag(this HtmlTextWriter writer, string tag) {
        writer.RenderBeginTag(tag);
        writer.RenderEndTag();
    }
     
    /// <summary>
    /// Renders the tag.
    /// </summary>
    /// <param name="writer">The writer.</param>
    /// <param name="tag">The tag.</param>
    /// <param name="content">The content.</param>
    public static void RenderTag(this HtmlTextWriter writer, HtmlTextWriterTag tag, string content) {
     
        writer.RenderBeginTag(tag);
        writer.Write(content);
        writer.RenderEndTag();
    }
     
    /// <summary>
    /// Renders the tag.
    /// </summary>
    /// <param name="writer">The writer.</param>
    /// <param name="tag">The tag.</param>
    /// <param name="content">The content.</param>
    public static void RenderTag(this HtmlTextWriter writer, string tag, string content) {
     
        writer.RenderBeginTag(tag);
        writer.Write(content);
        writer.RenderEndTag();
    }

    Now using those extension methods the sample lines of code above will become:

    writer.AddAttribute(HtmlTextWriterAttribute.Id, fieldName + "_Error");
    writer.AddAttribute(HtmlTextWriterAttribute.Class, "field-validation-error");
    writer.RenderTag(HtmlTextWriterTag.Span);

    and:

    writer.RenderTag(HtmlTextWriterTag.Span, "allow editing");


    Hope this helps.

    Regards,
    Velio