learn forever RSS 2.0
# Saturday, May 01, 2010

If you’re an ASP.NET MVC developer, you’ve almost undoubtedly used HTML helpers, or at least the built in ones.

Here’s are some common ones:

  1. <%=Html.DropDownList("Countries","Choose Country")%>

This generates a DropDownList populated with the property “Countries” of type “SelectList” on the ViewData model passed into the view.

  1. <%=Html.TextBox("EntitySearch")%>

This generates a TextBox bound to the property “EntitySearch” on the ViewData model passed into the view.  Can also be used to populate a property with the same name upon a form post.

In my current project, I’ve found the need to have many different buttons. These buttons are custom in the sense that they aren’t the standard HTML buttons, they have CSS and/or images associated with them. Here’s how I might declare a couple of them right now:

  1. <input type = "image" class = "addnew-button" />

  1. <%=Html.SubmitImage("Submit","~/images/submit-button.png") %>

So in both cases, I have to type in a string that kind of defines my button. In the first example, I have to say my button is an “image” and the class to use to style the button is called “addnew-button”.  

In the second case, I have to define the actual path to the button.  It’d be nice to have a more common and type safe way to declare all of my buttons, so I’ve created a Button HtmlHelper method.  My goal is to be able to have code in my view like this:

  1. <%=Html.Button(ButtonHtmlHelper.ButtonType.AddNew) %>

  1. <%=Html.Button(ButtonHtmlHelper.ButtonType.Submit) %>

To do this, I created an extension method on the HtmlHelper class called button.  This extension method can handle all my button types and has some overloads to accept a client id and any additional custom styling.

  1. namespace MyProject.HtmlHelpers
  2. {
  3.     public static class ButtonHtmlHelper
  4.     {
  5.         public enum ButtonType { AddNew, Remove, Ok, Cancel, Submit, Save, Search, Clear, Continue, Active, Inactive }
  6.  
  7.         public static string Button(this HtmlHelper htmlHelper, ButtonType buttonType)
  8.         {
  9.             return Button(htmlHelper, buttonType, null, null);
  10.         }
  11.  
  12.         public static string Button(this HtmlHelper htmlHelper, ButtonType buttonType, string id)
  13.         {
  14.             return Button(htmlHelper, buttonType, id, null);
  15.         }
  16.  
  17.         public static string Button(this HtmlHelper htmlHelper, ButtonType buttonType, string id, string inlineCSS)
  18.         {
  19.             StringBuilder addNewButtonHTML = new StringBuilder();
  20.             addNewButtonHTML.Append(@");
  21.             addNewButtonHTML.Append(GetButtonTypeCSSClassPrefix(buttonType));
  22.             addNewButtonHTML.Append(@"-button"" type = ""image"" ");
  23.  
  24.             if (!string.IsNullOrEmpty(id))
  25.                 addNewButtonHTML.AppendFormat(@"id = ""{0}"" ", id);
  26.  
  27.             if (!string.IsNullOrEmpty(inlineCSS))
  28.                 addNewButtonHTML.AppendFormat(@"style = ""{0}"" ", inlineCSS);
  29.  
  30.             addNewButtonHTML.Append("/>");
  31.  
  32.             return addNewButtonHTML.ToString();
  33.         }
  34.  
  35.         private static string GetButtonTypeCSSClassPrefix(ButtonType buttonType)
  36.         {
  37.             switch (buttonType)
  38.             {
  39.                 case ButtonType.AddNew:
  40.                     return "addnew";
  41.                 case ButtonType.Cancel:
  42.                     return "cancel";
  43.                 case ButtonType.Ok:
  44.                     return "ok";
  45.                 case ButtonType.Remove:
  46.                     return "remove";
  47.                 case ButtonType.Submit:
  48.                     return "submit";
  49.                 case ButtonType.Save:
  50.                     return "save";
  51.                 case ButtonType.Search:
  52.                     return "search";
  53.                 case ButtonType.Clear:
  54.                     return "clear";
  55.                 case ButtonType.Continue:
  56.                     return "continue";
  57.                 case ButtonType.Active:
  58.                     return "active";
  59.                 case ButtonType.Inactive:
  60.                     return "inactive";
  61.                 default:
  62.                     throw new ArgumentException(string.Format("Unknown ButtonType of {0} was passed in.", buttonType));
  63.             }
  64.         }
  65.     }
  66. }

 

That’s all! Just make sure you either add a using statement on the view to the namespace where the HTMLHelper is, or put it in the web.config like I did in my project. (line 10)

  1.     <namespaces>
  2.         <add namespace="System.Web.Mvc"/>
  3.         <add namespace="System.Web.Mvc.Ajax"/>
  4.         <add namespace="System.Web.Mvc.Html"/>
  5.         <add namespace="System.Web.Routing"/>
  6.         <add namespace="System.Linq"/>
  7.         <add namespace="System.Collections.Generic"/>
  8. <add namespace="Microsoft.Web.Mvc"/>
  9. <add namespace="xVal.Html"/>
  10. <add namespace="MyProject.HtmlHelpers"/>
  11.     namespaces>
Saturday, May 01, 2010 4:17:20 AM (GMT Daylight Time, UTC+01:00)  #    Comments [0] - Trackback
ASP.Net MVC | HTMLHelper
Dave Arlin
Archive
<May 2010>
SunMonTueWedThuFriSat
2526272829301
2345678
9101112131415
16171819202122
23242526272829
303112345
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2010
Dave Arlin
Sign In
Statistics
Total Posts: 9
This Year: 2
This Month: 0
This Week: 0
Comments: 0
All Content © 2010, Dave Arlin
DasBlog theme 'Business' created by Christoph De Baene (delarou)