Introduction

In this article we will learn about how to create Custom HotSpots in ASP.NET as well as the usage of JavaScript to highlight the hotspot area on mouseover and mouseleave event.

In the previous article Creating HotSpots in ASP.NET, I had explained about the different ways to create HotSpots. So if you're not aware about the term 'HotSpots' in ASP.NET, you may read more about it from here.
Part 1
RECAP

Basically, HotSpot is a part of a single image which may produce some actions when anyone click or move the mouse pointer over that hotspot (area). It's the part of graphics design which is used for complex web page design.

We can create any numbers of hotspots on a image by using HotSpot object inside the ImageMap control. ASP.NET has the following three basic types of HotSpot that correspond to the <area> tag defined by HTML.
  • RectangleHotSpot
  • CircleHotSpot
  • PolygonHotSpot
Custom Hotspots

Now we're going to learn about Custom HotSpots. As we have read in the recap paragraph, we can create any type of clickable region by using RectangleHotSpot, CircleHotSpot and PolygonHotSpot. In this article we will create a custom hotspot for the following images.
Triangle
Figure 1 : HotSpot over triangular area
NewYear
Figure 2 : HotSpot over polygon area (2016)
Note:

Further, we will read how can we create a custom hotspot for triangular and polygon shapes. So I will create custom hotspots to make the triangular area of first image clickable as well the number 2016 as clickable.

Basically these are the three derived class in ASP.NET which derives from HotSpot base class. We can create a variety of complex multi-sided shapes, such as triangular, octagons, diamonds and so on.

Note

ImageMap control supports any HotSpot- derived class but we cannot do anything that falls outside the HTML standard.
Important Points
When creating a custom HotSpot
1. Our class must be derived from HotSpot.

Example

Point1
2. Override MarkupName.

We must override the 'MarkupName' property to return the type of shape we are creating. We can use circle, rectangle or polygon as a return value. This information is place into 'shape' attribute of <area> tag.

Example:

Point2
3. GetCoordinates()

We need to override the GetCoordinates() method which return the coordinates of hotspot with comma separated. For a polygon, it must be a comma-separated series of points in X,Y pairs.

Example

Point3
4. Before using our custom hotspot, we must register it's namespace at page or config file as common custom control of ASP.NET.

Example

Point4
5.
After following these 4 steps our custom HotSpot will be ready to use with ImageMap control. Now let's see the practical implementation in a website.
Steps to create Custom Hotspots

Step 1: Open your Visual Studio and create a new website. For this article I am using Visual Studio 2012 and going to create an 'Empty Website'.

NewWebsite

Step 2:
Now add App_Code folder in your application.

Step 3:
Now add a class inside the App_code folder and do the name of class for your custom hotspot and use the ASP.NET HotSpot class as its base class.

Note: It's not mandatory to create your class inside the App_Code.

Firstly, I am going to create a custom hotspot for triangular shape. Let's see the following code snippet.

Code Snippet
  1. namespace MyCustomHotSpot
  2. {
  3. public class TriangleHotSpot : HotSpot
  4. {
  5. // ...
  6. }
  7. }
  • HotSpot is the class provided by ASP.NET.
  • TrinagelHotSpot is our custom hotspot class name.
  • MyCustomHotSpot is the name of namespace which I am using for this article.
Note: We need to add the namespace System.Web.UI.WebControls;
Step 4: Now we need to define some properties which are necessary to draw a triangle. Let's see the following code snippet in which I have used X, Y, Height and Width properties to get or set the values of X,Y coordinates and height & width.

Code Snippet
  1. public TriangleHotSpot()
  2. {
  3. X = 0;
  4. Y = 0;
  5. Height = 0;
  6. Width = 0;
  7. }
  8. public string CssClass { get; set; }
  9. public int X
  10. {
  11. get { return (int)ViewState["X"]; }
  12. set { ViewState["X"] = value; }
  13. }
  14. public int Y
  15. {
  16. get { return (int)ViewState["Y"]; }
  17. set { ViewState["Y"] = value; }
  18. }
  19. public int Height
  20. {
  21. get { return (int)ViewState["Height"]; }
  22. set { ViewState["Height"] = value; }
  23. }
  24. public int Width
  25. {
  26. get { return (int)ViewState["Width"]; }
  27. set { ViewState["Width"] = value; }
  28. }
Step 5: Now we need to override the 'MarkupName' property and GetCoordinates() method in this class.

Code Snippet
  1. protected override string MarkupName
  2. {
  3. get { return "polygon"; }
  4. }
  5. public override string GetCoordinates()
  6. {
  7. //Top coordinates
  8. int topXvalue = X;
  9. int topYvalue = Y - Height / 2;
  10. //Bottom-Left Cordinates
  11. int bottomLeftXvalue = X - Width / 2;
  12. int bottomLeftYvalue = Y + Height / 2;
  13. //Bottom-Right coordinates
  14. int bottomRightXvalue = X + Width / 2;
  15. int bottomRightYvalue = Y + Height / 2;
  16. var coordinateValues = @"" + topXvalue + "," + topYvalue + "," +
  17. bottomLeftXvalue + "," + bottomLeftYvalue + "," +
  18. bottomRightXvalue + "," + bottomRightYvalue;
  19. return coordinateValues;

The following is the complete code snippet for custom hotspot class "TriangleHotSpot".
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI.WebControls;
  6. namespace MyCustomHotSpots
  7. {
  8. public class TriangleHotSpot: HotSpot
  9. {
  10. public TriangleHotSpot()
  11. {
  12. X = 0;
  13. Y = 0;
  14. Height = 0;
  15. Width = 0;
  16. }
  17. public string CssClass { get; set; }
  18. public int X
  19. {
  20. get { return (int)ViewState["X"]; }
  21. set { ViewState["X"] = value; }
  22. }
  23. public int Y
  24. {
  25. get { return (int)ViewState["Y"]; }
  26. set { ViewState["Y"] = value; }
  27. }
  28. public int Height
  29. {
  30. get { return (int)ViewState["Height"]; }
  31. set { ViewState["Height"] = value; }
  32. }
  33. public int Width
  34. {
  35. get { return (int)ViewState["Width"]; }
  36. set { ViewState["Width"] = value; }
  37. }
  38. protected override string MarkupName
  39. {
  40. get { return "polygon"; }
  41. }
  42. public override string GetCoordinates()
  43. {
  44. //Top coordinates
  45. int topXvalue = X;
  46. int topYvalue = Y - Height / 2;
  47. //Bottom-Left Cordinates
  48. int bottomLeftXvalue = X - Width / 2;
  49. int bottomLeftYvalue = Y + Height / 2;
  50. //Bottom-Right coordinates
  51. int bottomRightXvalue = X + Width / 2;
  52. int bottomRightYvalue = Y + Height / 2;
  53. var coordinateValues = @"" + topXvalue + "," + topYvalue + "," +
  54. bottomLeftXvalue + "," + bottomLeftYvalue + "," +
  55. bottomRightXvalue + "," + bottomRightYvalue;
  56. return coordinateValues;
  57. }
  58. }
  59. }
Step 6: Our custom hotspot 'TriangleHotSpot' is ready. Now we will use it inside the ImageMap control. So let’s add a web page in your application. For this article I am going to add a new web page with name: Triangle.aspx

Step 7: Now we need to register the namespace of custom hotspot 'TriangleHotSpot'. We can register it on TRiangle.aspx page or configuration file web.config). For this article I am going to register on page directly.
  1. <%@ Register Namespace="MyCustomHotSpot" TagPrefix="myHotSpot" %>
Step 8: Now add an ImageMap control on this page and refer the image on which you want to create hotspots. (For this article I am going to refer an image which has a shape of triangle. Here we will create a hotspot over triangular area only.)

Triangle.aspx
  1. <asp:ImageMap ID="ImageMap1" runat="server" ImageUrl="~/Images/Triangle.png">
  2. <myHotSpot:TriangleHotSpot X="265" Y="125" Height="155" Width="185"
  3. AlternateText="triangle" />
  4. </asp:ImageMap>
Note

In this article I am going to use JavaScript to change the image state on mouseover, but if you want use click event then you may use HotSpotMode="PostBack" with PostBackValue="your value" Click here to know about HotSpotMode.
Step 9: Now add JQuery file in your solution, For this article I am going to create a folder with name JavaScript where I will put the jQuery
file jquery-1.7.1.js.

JqueryFile

Now add the reference of this jQuery file on Triangle.aspx page and write the following JavaScript code snippet inside the <head> tag of web page like the following,
  1. <script src="JavaScript/jquery-1.7.1.js"></script>
  2. <script type="text/javascript">
  3. jQuery(document).ready(function () {
  4. //On mouseover : change the image
  5. jQuery("area[shape='polygon']").mouseover(function () {
  6. jQuery("img").attr('src', 'Images/HoverImages/TriangleHover.png');
  7. });
  8. // On mouseleave set the default image
  9. jQuery("area[shape='polygon']").mouseleave(function () {
  10. jQuery("img").attr('src', 'Images/Triangle.png');
  11. });
  12. });
  13. </script>
Now execute / run the Triangle.aspx page to see the output.

Output

Output1
Similarly, I am going to create another HotSpot for the Figure 2 image (2016), Add another class inside the App_Code with name CustomPolygon.cs.

Code Snippet
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI.WebControls;
  6. namespace MyCustomHotSpots
  7. {
  8. public class CustomPolygon : HotSpot
  9. {
  10. public CustomPolygon()
  11. {
  12. //Do here
  13. }
  14. //Property
  15. public string CoordinateValues
  16. {
  17. get { return (string)ViewState["value"]; }
  18. set { ViewState["value"] = value; }
  19. }
  20. //Overridden Property
  21. protected override string MarkupName
  22. {
  23. get { return "Polygon"; }
  24. }
  25. //Overridden Method
  26. public override string GetCoordinates()
  27. {
  28. return CoordinateValues;
  29. }
  30. }
  31. }
Now add another web page with name "Polygon.aspx" and register the control.
  1. <%@ Register Namespace="MyCustomHotSpots" TagPrefix="myHotSpot" %>
Now add ImageMap control with Custom Polygon object.
  1. <form id="form1" runat="server">
  2. <asp:ImageMap ID="ImageMap1" runat="server" ImageUrl="~/Images/2016.png">
  3. <myHotSpot:CustomPolygon AlternateText="Two"
  4. CoordinateValues="51,102;52,55;57,47,129,45;137,53;138,100;81,204;137,204;137,226;49,226;49,216;111,102;110,66;77,68;77,100;51,102" />
  5. <myHotSpot:CustomPolygon AlternateText="Zero"
  6. CoordinateValues="218,225;206,217;206,57;217,46;287,46;295,57;295,215;285,226;218,225;231,204;231,68;268,68;268,204;232,204" />
  7. <myHotSpot:CustomPolygon AlternateText="One"
  8. CoordinateValues="375,228;401,228;401,46;378,46;363,96;375,96;375,228" />
  9. <myHotSpot:CustomPolygon AlternateText="Six"
  10. CoordinateValues="473,217;473,56;482,46;549,45;560,57;560,99;535,99;535,68;499,68;499,123;552,124;560,131;560,219;548,226;484,227;473,219;498,205;500,146;535,146;535,204;499,205" />
  11. </asp:ImageMap>
  12. /form>
In the above code snippet I have used four CustomPolygon object for the numbers 2 ,0, 1 and 6.

Now add the following JavaScript code inside the <head> of Polygon.aspx.
  1. <script src="JavaScript/jquery-1.7.1.js"></script>
  2. <script type="text/javascript">
  3. jQuery(document).ready(function () {
  4. //Set Hover image for 2 as highlighted
  5. jQuery("area[title='Two']").mouseover(function () {
  6. jQuery("img").attr('src', 'Images/HoverImages/Two.png');
  7. });
  8. //Set Hover image for 0 as highlighted
  9. jQuery("area[title='Zero']").mouseover(function () {
  10. jQuery("img").attr('src', 'Images/HoverImages/Zero.png');
  11. });
  12. //Set Hover image for 1 as highlighted
  13. jQuery("area[title='One']").mouseover(function () {
  14. jQuery("img").attr('src', 'Images/HoverImages/One.png');
  15. });
  16. //Set Hover image for 6 as highlighted
  17. jQuery("area[title='Six']").mouseover(function () {
  18. jQuery("img").attr('src', 'Images/HoverImages/Six.png');
  19. });
  20. //To Set Default Image
  21. jQuery("area").mouseleave(function () {
  22. jQuery("img").attr('src', 'Images/2016.png');
  23. });
  24. });
  25. </script>
Output for CustomPolygon HotSpot.

Output2For2016

Summary

I this article we read about how to create custom HotSpots as well as how to use JavaScript to set some actions over the hotspot areas.
In the previous article I had explained about the HotSpotMode to make the HotSpot area as clickable so if you're not aware about it visit here.