Click here to Skip to main content
Licence CPOL
First Posted 12 Aug 2009
Views 9,092
Bookmarked 11 times

HTML Helpers with Extension Methods

By | 13 Aug 2009 | Article
In this article, I will explore HTML Helpers with Extension Methods

Introduction

In the last article, I explored Creating Custom HTML Helpers by creating a static method that returns a string. In this article, I will explore HTML Helpers with Extension Methods. The HtmlHelper class provides a set of helper methods that generate plain HTML and return the result as a string. The extensions add helper methods for creating forms, rendering HTML controls and rendering partial views and they are located in the System.Web.Mvc.Html namespace. I will create a submit confirm button that renders an HTML <input type=”submit”> tag as SubmitConfirmHelper class shown below:

using System.Web.Mvc;

namespace Helpers
{
   public static class SubmitConfirmHelper
   {
       /// <summary> 
       /// Renders an HTML form submit confirm button
       /// </summary> 
       public static string SubmitConfirm(this HtmlHelper helper,
           string buttonText, string alertMessage)
       {
           return String.Format(
           "<input type=\"submit\" value=\"{0}\" onClick=\"return confirm('{1}');\" />",
           buttonText, alertMessage);
       }
   }
}

Because the SubmitConfirm() method extends the HtmlHelper class, this method appears as a method of the HtmlHelper class in Intellisense as shown below:

image001.jpg

The view uses the new Html.SubmitConfirm() helper to render the submit button for a form as shown below:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" 
    Inherits="System.Web.Mvc.ViewPage" %>
<%@ Import Namespace="Helpers"%>
<asp:Content ID="aboutTitle" ContentPlaceHolderID="TitleContent" runat="server">
    About Us
</asp:Content>
 
<asp:Content ID="aboutContent" ContentPlaceHolderID="MainContent" runat="server">
    <h2>About</h2>
    <p>
    <% using (Html.BeginForm()) { %>
      <%=  Html.SubmitConfirm("Delete", "Do you want to delete?")%>
    </p>
     <% } %>
</asp:Content>

Now you can run the project and it will render the form as shown below:

image002.jpg

Summary

In this article, you learned a method of creating a custom HTML Helper by extending the HtmlHelper class.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Farooq Kaiser

Software Developer (Senior)
http://www.Fairnet.com
Canada Canada

Member

12+ years of complete software development life cycle experience for web based applications and multi-tier client-server desktop, primarily using LINQ, WCF, WWF, C#, ASP.NET, XML, XSLT, AJAX, Winforms,Visual Basic, JavaScript, JQuery, Google APIs, C++, VB.NET, C, ATL/COM, Open XML. Extensively involved in the requirement analysis, feasibility study, conceptualization, planning, architecture/design, configuration, development, quality assurance, implementation and release of the software products.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralGood Article PinmemberDonsw10:51 13 Jun '10  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 13 Aug 2009
Article Copyright 2009 by Farooq Kaiser
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid