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:

  • AddCssClass() - Enables   you to add a new class=”” attribute to a tag.
  • GenerateId() - Enables   you to add an id attribute to a tag. This method automatically replaces   periods in the id (by default, periods are replaced by underscores)
  • MergeAttribute() - Enables   you to add attributes to a tag. There are multiple overloads of this   method.
  • SetInnerText() - Enables   you to set the inner text of the tag. The inner text is HTML encode   automatically.
  • ToString() - Enables you   to render the tag. You can specify whether you want to create a normal   tag, a start tag, an end tag, or a self-closing tag.

  The TagBuilder class has four important  properties:

  • Attributes - Represents   all of the attributes of the tag.
  • IdAttributeDotReplacement   - Represents the character used by the GenerateId() method to replace   periods (the default is an underscore).
  • InnerHTML - Represents   the inner contents of the tag. Assigning a string to this property   does not HTML encode the string.
  • TagName - Represents the   name of the tag.

  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\ImageHelper.cs

  

using System.Web.Mvc;
using System.Web.Routing; namespace MvcApplication1.Helpers
{
public static class ImageHelper
{
public static string Image(this HtmlHelper helper, string id, string url, string alternateText)
{
return Image(helper, id, url, alternateText, null);
} public static string Image(this HtmlHelper helper, string id, string url, string alternateText, object htmlAttributes)
{
// Create tag builder
var builder = new TagBuilder("img"); // Create valid id
builder.GenerateId(id); // Add attributes
builder.MergeAttribute("src", url);
builder.MergeAttribute("alt", alternateText);
builder.MergeAttributes(new RouteValueDictionary(htmlAttributes)); // Render tag
return builder.ToString(TagRenderMode.SelfClosing);
} }
}

  The class in Listing 1 contains two  static 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\Index.aspx

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<%@ Import Namespace="MvcApplication1.Helpers" %> <asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server"> <!-- Calling helper without HTML attributes -->
<%= Html.Image("img1", ResolveUrl("~/Content/XBox.jpg"), "XBox Console") %> <!-- Calling helper with HTML attributes -->
<%= Html.Image("img1", ResolveUrl("~/Content/XBox.jpg"), "XBox Console", new {border="4px"})%> </asp:Content>

  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 (指示符):

<%@ Import Namespace="MvcApplication1.Helpers" %>

原谅网址http://www.asp.net/mvc/tutorials/older-versions/views/using-the-tagbuilder-class-to-build-html-helpers-cs

最新文章

  1. Qt文件路径分隔符
  2. Java数据结构之字符串模式匹配算法---Brute-Force算法
  3. bzoj3876: [Ahoi2014]支线剧情
  4. maven 简介
  5. ajax跟取后台 josn 之 josn理解
  6. 【干货分享】Google 的设计准则,素材和资源
  7. C#源码500份
  8. Android中突发情况Activity数据的保存和恢复
  9. 仿酷狗音乐播放器开发日志二十四 选项设置窗体的实现(附328行xml布局源码)
  10. IXListView的自我分析一
  11. c++之 scanf 接收用户输入内容
  12. log4j日志输出配置
  13. Java Swing TextArea 滚动条和获得焦点
  14. CLR之委托的揭秘(二)
  15. centos7服务的管理
  16. C#学习笔记 day_two
  17. LoadRunner【第二篇】原理及使用流程
  18. 一套能体现 RBAC 的表结构设计
  19. ORACLE的数据类型的长度合集
  20. Nginx+Keepalived 实现高可用

热门文章

  1. Yii render和renderPartial的区别
  2. C# 获取随机可用端口号
  3. Cordova 设置全屏及退出全屏切换
  4. angular中实现jQuery的Document Ready
  5. 3G 2G GPRS 1G的概念
  6. 【UVA 10600】 ACM Contest and Blackout(最小生成树和次小生成树)
  7. 【BZOJ 1010】 [HNOI2008]玩具装箱toy (斜率优化)
  8. WIN32和Kernel)直接读写硬盘扇区
  9. VC模拟发送数据包-百度关键词查找
  10. Android 获取图片资源的4种方式