logo

Imagelink HTML helper class for Asp.net MVC

By Vasanthakumar D
Printer Friendly Version
View My Articles
426 Views
    

In this article, I will give you the HTML helper class for Imagelink. Its very simple and straight forward one and easy to understand.


Helper Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace MvcApplication1.Helpers
{
    public static class ImageHelper
    {
        /// <summary>
        /// return image link
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="id">Id of link control</param>
        /// <param name="controller">target controller name</param>
        /// <param name="action">target action name</param>
        /// <param name="strOthers">other URL parts like querystring, etc</param>
        /// <param name="strImageURL">URL for image</param>
        /// <param name="alternateText">Alternate Text for the image</param>
        /// <param name="strStyle">style of the image like border properties, etc</param>
        /// <returns></returns>
        public static string ImageLink(this HtmlHelper helper, string id, string controller, string action, string strOthers, string strImageURL, string alternateText, string strStyle)
        {
            return ImageLink(helper, id, controller, action, strOthers, strImageURL, alternateText,strStyle,null);
        }



/// <summary> /// return image link /// </summary> /// <param name="helper"></param> /// <param name="id">Id of link control</param> /// <param name="controller">target controller name</param> /// <param name="action">target action name</param> /// <param name="strOthers">other URL parts like querystring, etc</param> /// <param name="strImageURL">URL for image</param> /// <param name="alternateText">Alternate Text for the image</param> /// <param name="strStyle">style of the image like border properties, etc</param> /// <param name="htmlAttributes">html attribues for link</param> /// <returns></returns> public static string ImageLink(this HtmlHelper helper, string id, string controller, string action, string strOthers, string strImageURL, string alternateText,string strStyle, object htmlAttributes) { // Create tag builder var builder = new TagBuilder("a"); // Create valid id builder.GenerateId(id); // Add attributes builder.MergeAttribute("href", "/" + controller + "/" + action + strOthers); //form target URL builder.InnerHtml = "<img src='" + strImageURL + "' alt='" + alternateText + "' style=\"" + strStyle + "\">"; //set the image as inner html builder.MergeAttributes(new RouteValueDictionary(htmlAttributes)); // Render tag return builder.ToString(TagRenderMode.Normal); //to add </a> as end tag } } }

In View Pages


<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<%@ Import Namespace="MvcApplication1.Helpers" %> - importing namespace here

<asp:Content ID="indexTitle" ContentPlaceHolderID="TitleContent" runat="server">
    Home Page
</asp:Content>

<asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">
  <% using (Html.BeginForm()) { %>
    <h2><%= Html.Encode(ViewData["Message"]) %></h2>
    <p>
        To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">http://asp.net/mvc</a>.
    </p>
      
Adding link image link here by passing Id, controller name, action name, Image url, alternate text, style <%= Html.ImageLink("hTest","Grid","Show","/20",ResolveUrl("~/Content/blarg.gif"),"test image","border:none;")%> </asp:Content>

I hope this article helpful and self explanatory. Please post your comments and suggestions.



Didn't Find The Answer You Were Looking For?

EggHeadCafe has experts online right now that may know the answer to your question.  We pay them a bonus for answering as many questions as they can.  So, why not help them and yourself by becoming a member (free) and ask them your question right now?
Ask Question In Live Forum

If you have an OpenID and do not want to become a member of the EggHeadCafe forum, you can also sign on to Chat Chaos and post your question to our real time Silverlight chat application.
Ask Question In Chat Chaos

Article Discussion: Imagelink HTML helper class for Asp.net MVC
Vasanthakumar D posted at Tuesday, May 05, 2009 4:25 AM
Original Article
 






  $1000 Contest    [)ia6l0 iii - $228  |  Jonathan VH - $161  |  Huggy Bear - $135  |  F Cali - $95  |  egg egg - $94  |  more Advertise  |  Privacy  |   (c) 2010