"
ASP.NET (snapshot 2017) Microsoft documentation and samples

Using the TagBuilder Class to Build HTML Helpers (VB)

by Stephen Walther

Stephen Walther introduces you to a useful utility class in the ASP.NET MVC framework named the TagBuilder class. You can use the TagBuilder class to easily build HTML tags.

The ASP.NET MVC framework includes a useful utility class named the TagBuilder class that you can use when building HTML helpers. The TagBuilder class, as the name of the class suggests, enables you to easily build HTML tags. In this brief tutorial, you are provided with an overview of the TagBuilder class and you learn how to use this class when building a simple HTML helper that renders HTML <img> tags.

Overview of the TagBuilder Class

The TagBuilder class is contained in the System.Web.Mvc namespace. It has five methods:

The TagBuilder class has four important properties:

These methods and properties give you all of the basic methods and properties that you need to build up an HTML tag. You don’t really need to use the TagBuilder class. You could use a StringBuilder class instead. However, the TagBuilder class makes your life a little easier.

Creating an Image HTML Helper

When you create an instance of the TagBuilder class, you pass the name of the tag that you want to build to the TagBuilder constructor. Next, you can call methods like the AddCssClass and MergeAttribute() methods to modify the attributes of the tag. Finally, you call the ToString() method to render the tag.

For example, Listing 1 contains an Image HTML helper. The Image helper is implemented internally with a TagBuilder that represents an HTML <img> tag.

Listing 1 – Helpers.vb

[!code-vbMain]

   1:  Public Module ImageHelper
   2:   
   3:  <System.Runtime.CompilerServices.Extension> _
   4:  Function Image(ByVal helper As HtmlHelper, ByVal id As String, ByVal url As String, ByVal alternateText As String) As String
   5:      Return Image(helper, id, url, alternateText, Nothing)
   6:  End Function
   7:   
   8:  <System.Runtime.CompilerServices.Extension> _
   9:  Function Image(ByVal helper As HtmlHelper, ByVal id As String, ByVal url As String, ByVal alternateText As String, ByVal htmlAttributes As Object) As String
  10:      ' Create tag builder
  11:      Dim builder = New TagBuilder("img")
  12:   
  13:      ' Create valid id
  14:      builder.GenerateId(id)
  15:   
  16:      ' Add attributes
  17:      builder.MergeAttribute("src", url)
  18:      builder.MergeAttribute("alt", alternateText)
  19:      builder.MergeAttributes(New RouteValueDictionary(htmlAttributes))
  20:   
  21:      ' Render tag
  22:      Return builder.ToString(TagRenderMode.SelfClosing)
  23:  End Function
  24:   
  25:  End Module

The module in Listing 1 contains two overloaded methods named Image(). When you call the Image() method, you can pass an object which represents a set of HTML attributes or not.

Notice how the TagBuilder.MergeAttribute() method is used to add individual attributes such as the src attribute to the TagBuilder. Notice, furthermore, how the TagBuilder.MergeAttributes() method is used to add a collection of attributes to the TagBuilder. The MergeAttributes() method accepts a Dictionary<string,object> parameter. The The RouteValueDictionary class is used to convert the Object representing the collection of attributes into a Dictionary<string,object>.

After you create the Image helper, you can use the helper in your ASP.NET MVC views just like any of the other standard HTML helpers. The view in Listing 2 uses the Image helper to display the same image of an Xbox twice (see Figure 1). The Image() helper is called both with and without an HTML attribute collection.

Listing 2 – Home.aspx

[!code-aspxMain]

   1:  <%@ Page Language="VB" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
   2:  <%@ Import Namespace="MvcApplication1" %>
   3:  <asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">
   4:   
   5:      <!-- Calling helper without HTML attributes -->
   6:      <%= Html.Image("img1", ResolveUrl("~/Content/XBox.jpg"), "XBox Console") %>
   7:   
   8:      <!-- Calling helper with HTML attributes -->
   9:      <%= Html.Image("img1", ResolveUrl("~/Content/XBox.jpg"), "XBox Console", New With {.border="4px"})%>
  10:   
  11:  </asp:Content>

The New Project dialog box

Figure 01: Using the Image helper(Click to view full-size image)

Notice that you must import the namespace associated with the Image helper at the top of the Index.aspx view. The helper is imported with the following directive:

[!code-aspxMain]

   1:  <%@ Import Namespace="MvcApplication1" %>

In a Visual Basic application, the default namespace is the same as the name of the application.

Previous Next



Comments ( )
Link to this page: //www.vb-net.com/AspNet-DocAndSamples-2017/aspnet/mvc/overview/older-versions-1/views/using-the-tagbuilder-class-to-build-html-helpers-vb.htm
< THANKS ME>