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

Creating Custom HTML Helpers (VB)

by Microsoft

Download PDF

The goal of this tutorial is to demonstrate how you can create custom HTML Helpers that you can use within your MVC views. By taking advantage of HTML Helpers, you can reduce the amount of tedious typing of HTML tags that you must perform to create a standard HTML page.

The goal of this tutorial is to demonstrate how you can create custom HTML Helpers that you can use within your MVC views. By taking advantage of HTML Helpers, you can reduce the amount of tedious typing of HTML tags that you must perform to create a standard HTML page.

In the first part of this tutorial, I describe some of the existing HTML Helpers included with the ASP.NET MVC framework. Next, I describe two methods of creating custom HTML Helpers: I explain how to create custom HTML Helpers by creating a shared method and by creating an extension method.

Understanding HTML Helpers

An HTML Helper is just a method that returns a string. The string can represent any type of content that you want. For example, you can use HTML Helpers to render standard HTML tags like HTML <input> and <img> tags. You also can use HTML Helpers to render more complex content such as a tab strip or an HTML table of database data.

The ASP.NET MVC framework includes the following set of standard HTML Helpers (this is not a complete list):

For example, consider the form in Listing 1. This form is rendered with the help of two of the standard HTML Helpers (see Figure 1). This form uses the Html.BeginForm() and Html.TextBox() Helper methods.

Page rendered with HTML Helpers

Figure 01: Page rendered with HTML Helpers (Click to view full-size image)

Listing 1 – Views\Home\Index.aspx

[!code-aspxMain]

   1:  <%@ Page Language="VB" AutoEventWireup="false" CodeBehind="Index.aspx.vb" Inherits="MvcApplication1.Index"%>
   2:  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   3:   
   4:  <html xmlns= "http://www.w3.org/1999/xhtml ">
   5:  <head id="Head1" runat="server">
   6:       <title>Index</title>
   7:  </head>
   8:  <body>
   9:        <div>
  10:       <% Using Html.BeginForm()
  11:            <label for="firstName">First Name:</label>
  12:   
  13:            <br />
  14:            <%= Html.TextBox("firstName")%>
  15:            <br /><br />
  16:            <label for="lastName">Last Name:</label>
  17:            <br />
  18:            <%= Html.TextBox("lastName")%>
  19:            <br /><br />
  20:   
  21:            <input type="submit" value="Register" />    
  22:       <% End Using %>
  23:       </div>
  24:  </body>
  25:  </html>

The Html.BeginForm() Helper method is used to create the opening and closing HTML <form> tags. Notice that the Html.BeginForm() method is called within a using statement. The using statement ensures that the <form> tag gets closed at the end of the using block.

If you prefer, instead of creating a using block, you can call the Html.EndForm() Helper method to close the <form> tag. Use whichever approach to creating an opening and closing <form> tag that seems most intuitive to you.

The Html.TextBox() Helper methods are used in Listing 1 to render HTML <input> tags. If you select view source in your browser then you see the HTML source in Listing 2. Notice that the source contains standard HTML tags.

[!IMPORTANT] notice that the Html.TextBox()-HTML Helper is rendered with <%= %> tags instead of <% %> tags. If you don’t include the equal sign, then nothing gets rendered to the browser.

The ASP.NET MVC framework contains a small set of helpers. Most likely, you will need to extend the MVC framework with custom HTML Helpers. In the remainder of this tutorial, you learn two methods of creating custom HTML Helpers.

Listing 2 – Index.aspx Source

[!code-aspxMain]

   1:  <%@ Page Language="VB" AutoEventWireup="false" CodeBehind="Index.aspx.vb" Inherits="MvcApplication1.Index" %>
   2:   
   3:  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   4:  <html xmlns="http://www.w3.org/1999/xhtml">
   5:  <head>
   6:       <title>Index</title>
   7:  </head>
   8:  <body>
   9:       <div>
  10:       <form action="http://localhost:33102/" method="post">
  11:   
  12:            <label for="firstName">First Name:</label>
  13:            <br />
  14:            <input id="firstName" name="firstName" type="text" value="" />
  15:            <br /><br />
  16:            <label for="lastName">Last Name:</label>
  17:            <br />
  18:   
  19:            <input id="lastName" name="lastName" type="text" value="" />
  20:            <br /><br />
  21:            <input id="btnRegister" name="btnRegister" type="submit" value="Register" />
  22:       </form>
  23:       </div>
  24:  </body>
  25:  </html>

Creating HTML Helpers with Shared Methods

The easiest way to create a new HTML Helper is to create a shared method that returns a string. Imagine, for example, that you decide to create a new HTML Helper that renders an HTML <label> tag. You can use the class in Listing 2 to render a <label>.

Listing 2 – Helpers\LabelHelper.vb

[!code-vbMain]

   1:  Public Class LabelHelper
   2:  Public Shared Function Label(ByVal target As String, ByVal text As String) As String
   3:       Return String.Format("<label for='{0}'>{1}</label>", target, text)
   4:  End Function
   5:  End Class

There is nothing special about the class in Listing 2. The Label() method simply returns a string.

The modified Index view in Listing 3 uses the LabelHelper to render HTML <label> tags. Notice that the view includes an <%@ imports %> directive that imports the Application1.Helpers namespace.

Listing 2 – Views\Home\Index2.aspx

[!code-aspxMain]

   1:  <%@ Page Language="VB" AutoEventWireup="false" CodeBehind="Index2.aspx.vb" Inherits="MvcApplication1.Index2"%>
   2:  <%@ Import Namespace="MvcApplication1" %>
   3:  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   4:  <html xmlns="http://www.w3.org/1999/xhtml" >
   5:  <head id="Head1" runat="server">
   6:       <title>Index2</title>
   7:  </head>
   8:   
   9:  <body>
  10:       <div>
  11:       <% Using Html.BeginForm()
  12:            <%= LabelHelper.Label("firstName", "First Name:") %>
  13:            <br />
  14:            <%= Html.TextBox("firstName") %>
  15:            <br /><br />
  16:            <%= LabelHelper.Label("lastName", "Last Name:") %>
  17:            <br />
  18:   
  19:            <%= Html.TextBox("lastName") %>
  20:            <br /><br />
  21:            <input type="submit" value="Register" />
  22:       <% End Using %>
  23:       </div>
  24:  </body>
  25:  </html>

Creating HTML Helpers with Extension Methods

If you want to create HTML Helpers that work just like the standard HTML Helpers included in the ASP.NET MVC framework then you need to create extension methods. Extension methods enable you to add new methods to an existing class. When creating an HTML Helper method, you add new methods to the HtmlHelper class represented by a view’s Html property.

The Visual Basic module in Listing 3 adds an extension method named Label() to the HtmlHelper class. There are a couple of things that you should notice about this module. First, notice that the module is decorated with the <Extension()> attribute. In order to use this attribute, you must import the System.Runtime.CompilerServices namespace

Second, notice that the first parameter of the Label() method represents the HtmlHelper class. The first parameter of an extension method indicates the class that the extension method extends.

Listing 3 – Helpers\LabelExtensions.vb

[!code-vbMain]

   1:  Imports System.Runtime.CompilerServices
   2:   
   3:  Public Module LabelExtensions
   4:       <Extension()> _
   5:       Public Function Label(ByVal helper As HtmlHelper, ByVal target As String, ByVal text As String) As String
   6:            Return String.Format("<label for='{0}'> {1}</label>", target, text)
   7:   
   8:       End Function
   9:  End Module

After you create an extension method, and build your application successfully, the extension method appears in Visual Studio Intellisense like all of the other methods of a class (see Figure 2). The only difference is that extension methods appear with a special symbol next to them (an icon of a downward arrow).

Using the Html.Label() extension method

Figure 02: Using the Html.Label() extension method (Click to view full-size image)

The modified Index view in Listing 4 uses the Html.Label() extension method to render all of its <label> tags.

Listing 4 – Views\Home\Index3.aspx

[!code-aspxMain]

   1:  <%@ Page Language="VB" AutoEventWireup="false" CodeBehind="Index3.aspx.vb" Inherits="MvcApplication1.Index3" %>
   2:   
   3:  <%@ Import Namespace="MvcApplication1" %>
   4:  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   5:  <html xmlns="http://www.w3.org/1999/xhtml" >
   6:  <head id="Head1" runat="server">
   7:       <title>Index3</title>
   8:  </head>
   9:  <body>
  10:       <div>
  11:   
  12:       <% Using Html.BeginForm()%>
  13:       <%=Html.Label("firstName", "First Name:")%>
  14:       <br />
  15:       <%= Html.TextBox("firstName")%>
  16:       <br /><br />
  17:       <%=Html.Label("lastName", "Last Name:")%>
  18:       <br />
  19:       <%= Html.TextBox("lastName")%>
  20:   
  21:       <br /><br />
  22:       <input type="submit" value="Register" />    
  23:       <% End Using%>
  24:       </div>
  25:  </body>
  26:  </html>

Summary

In this tutorial, you learned two methods of creating custom HTML Helpers. First, you learned how to create a custom Label() HTML Helper by creating a shared method that returns a string. Next, you learned how to create a custom Label() HTML Helper method by creating an extension method on the HtmlHelper class.

In this tutorial, I focused on building an extremely simple HTML Helper method. Realize that an HTML Helper can be as complicated as you want. You can build HTML Helpers that render rich content such as tree views, menus, or tables of database data.

Previous Next



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