Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 17th, 2012  |  syntax: None  |  size: 5.67 KB  |  hits: 28  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Implementing ASP.NET MVC Ajax.ActionLink with Image
  2. public static class ImageHelpers
  3. {
  4.     /// <summary>
  5.     /// return image link
  6.     /// </summary>
  7.     /// <param name="helper"></param>
  8.     /// <param name="id">Id of link control</param>
  9.     /// <param name="controller">target controller name</param>
  10.     /// <param name="action">target action name</param>
  11.     /// <param name="strOthers">other URL parts like query string, etc</param>
  12.     /// <param name="strImageURL">URL for image</param>
  13.     /// <param name="alternateText">Alternate Text for the image</param>
  14.     /// <param name="strStyle">style of the image like border properties, etc</param>
  15.     /// <returns></returns>
  16.     public static string ImageLink(this HtmlHelper helper, string id, string controller, string action, string strOthers, string strImageURL,
  17.         string alternateText, string strStyle, string cssClass = "imagelink")
  18.     {
  19.         return ImageLink(helper, id, controller, action, strOthers, strImageURL, alternateText, strStyle, null, cssClass);
  20.     }
  21.     /// <summary>
  22.     /// return image link
  23.     /// </summary>
  24.     /// <param name="helper"></param>
  25.     /// <param name="id">Id of link control</param>
  26.     /// <param name="controller">target controller name</param>
  27.     /// <param name="action">target action name</param>
  28.     /// <param name="strOthers">other URL parts like query string, etc</param>
  29.     /// <param name="strImageURL">URL for image</param>
  30.     /// <param name="alternateText">Alternate Text for the image</param>
  31.     /// <param name="strStyle">style of the image like border properties, etc</param>
  32.     /// <param name="htmlAttributes">html attributes for link</param>
  33.     /// <returns></returns>
  34.     public static string ImageLink(this HtmlHelper helper, string id, string controller, string action, string strOthers, string strImageURL,
  35.         string alternateText, string strStyle, object htmlAttributes, string cssClass = "imagelink")
  36.     {
  37.         var portalModel = ContextCache<PortalModel>.Get(ContextCache.PortalModelSessionCache);
  38.  
  39.         // Create tag builder
  40.         var divBuilder = new TagBuilder("div");
  41.         divBuilder.AddCssClass(cssClass);
  42.  
  43.         var aBuilder = new TagBuilder("a");
  44.  
  45.         // Create valid id
  46.         if (!string.IsNullOrEmpty(id))
  47.             aBuilder.GenerateId(id);
  48.  
  49.         // Add attributes
  50.         aBuilder.MergeAttribute("href", "/" + portalModel.PortalTag + "/" + controller + "/" + action + strOthers); //form target URL
  51.         aBuilder.InnerHtml = "<img src='" + strImageURL + "' alt='" + alternateText + "' class='" + cssClass + "' style='border: none;'/>" + alternateText; //set the image as inner html
  52.         aBuilder.MergeAttributes(new RouteValueDictionary(htmlAttributes));
  53.  
  54.         divBuilder.InnerHtml = aBuilder.ToString(TagRenderMode.Normal); //to add </a> as end tag
  55.  
  56.         // Render tag
  57.         return divBuilder.ToString(TagRenderMode.Normal);
  58.     }
  59. }
  60.        
  61. public static class ImageHelpers
  62. {
  63.     /// <summary>
  64.     /// return image link
  65.     /// </summary>
  66.     /// <param name="helper"></param>
  67.     /// <param name="id">Id of link control</param>
  68.     /// <param name="controller">target controller name</param>
  69.     /// <param name="action">target action name</param>
  70.     /// <param name="strOthers">other URL parts like query string, etc</param>
  71.     /// <param name="strImageURL">URL for image</param>
  72.     /// <param name="alternateText">Alternate Text for the image</param>
  73.     /// <param name="strStyle">style of the image like border properties, etc</param>
  74.     /// <returns></returns>
  75.     public static string ImageLink(this HtmlHelper helper, string id, string controller, string action, string strOthers, string strImageURL,
  76.         string alternateText, string strStyle, string cssClass = "imagelink")
  77.     {
  78.         return ImageLink(helper, id, controller, action, strOthers, strImageURL, alternateText, strStyle, null, cssClass);
  79.     }
  80.     /// <summary>
  81.     /// return image link
  82.     /// </summary>
  83.     /// <param name="helper"></param>
  84.     /// <param name="id">Id of link control</param>
  85.     /// <param name="controller">target controller name</param>
  86.     /// <param name="action">target action name</param>
  87.     /// <param name="strOthers">other URL parts like query string, etc</param>
  88.     /// <param name="strImageURL">URL for image</param>
  89.     /// <param name="alternateText">Alternate Text for the image</param>
  90.     /// <param name="strStyle">style of the image like border properties, etc</param>
  91.     /// <param name="htmlAttributes">html attributes for link</param>
  92.     /// <returns></returns>
  93.     public static string ImageLink(this HtmlHelper helper, string id, string controller, string action, string strOthers, string strImageURL,
  94.         string alternateText, string strStyle, object htmlAttributes, string cssClass = "imagelink")
  95.     {
  96.         var portalModel = ContextCache<PortalModel>.Get(ContextCache.PortalModelSessionCache);
  97.  
  98.         // Create tag builder
  99.         var divBuilder = new TagBuilder("div");
  100.         divBuilder.AddCssClass(cssClass);
  101.  
  102.         var aBuilder = new TagBuilder("a");
  103.  
  104.         // Create valid id
  105.         if (!string.IsNullOrEmpty(id))
  106.             aBuilder.GenerateId(id);
  107.  
  108.         // Add attributes
  109.         aBuilder.MergeAttribute("href", "/" + portalModel.PortalTag + "/" + controller + "/" + action + strOthers); //form target URL
  110.         aBuilder.InnerHtml = "<img src='" + strImageURL + "' alt='" + alternateText + "' class='" + cssClass + "' style='border: none;'/>" + alternateText; //set the image as inner html
  111.         aBuilder.MergeAttributes(new RouteValueDictionary(htmlAttributes));
  112.  
  113.         divBuilder.InnerHtml = aBuilder.ToString(TagRenderMode.Normal); //to add </a> as end tag
  114.  
  115.         // Render tag
  116.         return divBuilder.ToString(TagRenderMode.Normal);
  117.     }
  118. }