Click here to Skip to main content
14,302,993 members

HtmlMap Web Control

Rate this:
3.71 (4 votes)
Please Sign up or sign in to vote.
3.71 (4 votes)
12 Mar 2007CPOL
This article describes the HtmlMap Web Control. Some description and code realization are provided.


The .NET 2.0 class library contains a very helpful Web-control named ImageMap. It can contain an image and an HTML map for it. We can define some areas called HotSpot(s) and assign a shape (circle, rectangle, or polygon) and a corresponding reference for each hotspot. Everything works fine except one thing. We cannot operate with our image in client-side code, because it is hidden and its ID is assigned automatically.

HTML Image Maps

Image 1

Fig. 1. The Solar system Imagemap

First of all, let's remember how the image maps are organized in ordinary HTML. We need to include the image (via an <img> tag) and map (via a <map> tag) it in our HTML file. In the <img> tag, we must specify a URL to the image file and the map ID which we will use to map our image:

<img src="planets.jpg" width="513" height="649" border="0" usemap="#PlanetsMap" />

Then, we must specify the areas on the image and the corresponding references for the areas. Note that the ID attribute of the map tag is "PlanetsMap" and not "#PlanetsMap".

<map name="PlanetsMap" id="Map1">
<area shape="poly" coords="139,58,122,66,116,84,121,94,124,101,132,107,

  href="" alt="Mercury" />
<area shape="poly" coords="176,40,195,44,207,54,217,70,217,

    href="" alt="Venus" />
  <area shape="circle" coords="247,128,50" 

    href="" alt="Earth" />
  <area shape="circle" coords="340,88,18" 

    href="" alt="Moon" />  
  <area shape="circle" coords="337,191,36" 

    href="" alt="Mars" />
<area shape="poly" coords="391,254,438,269,457,285,473,306,

  href="" alt="Jupiter" />  
<area shape="poly" coords="157,331,175,330,201,341,222,350,240,357,

  href="" alt="Saturn" />
  <area shape="poly" coords="207,460,235,446,270,450,297,472,309,500,

   href="" alt="Uranus" />
<area shape="circle" coords="123,520,101" 


    alt="Neptune" />

The HtmlMap Web Control

To gain the same functionality in ASPX web-pages, the HtmlMap web-control was developed. It includes two classes and an enum:

public enum AreaShapesEnum
public class MapArea
public class HtmlMap : System.Web.UI.WebControls.WebControl 

AreaShapesEnum is used by the MapArea class, and represents the possible shapes of a given area of the map. It can take the following values:

  • POLY

The MapArea also includes the following members:

public List<int> CoordsGets or sets a System.Collections.Generic.List of coordinates for a given MapArea object.
public Uri HrefGets or sets the System.Uri for this MapArea object.
public string AltGets or sets the alternate text for this area object.
public Dictionary<string, string> AttributesGets or sets the System.Collections.Generic.Dictionary array of additional attributes of this MapArea object.

Using the last property allows defining a lot of client-side event handlers. For instance, to define the client-side event handler for the mouse-over event, just use the following code:

MapArea map = new MapArea();
map.Attributes.Add("onmouseover", "javascript:alert('mouseover');");

The HtmlMap control includes the following members:

public string NAMEGets or sets the ID and NAME attributes of the generated map tag.
public List<MapArea> AREASGets or sets a list of MapArea objects for this HtmlMap object.

The protected Render method of System.Web.UI.WebControls.WebControl is overridden in order to properly render the HtmlMap control and all its area into the HTML.

Using the HtmlMap Control

To use the HtmlMap control, simply create its instance and set the NAME property. Then, create a number of MapArea objects, fill their properties, and add them into the HtmlMap.AREAS list.

Here is an example of generating an HtmMap object and defining the MapArea for the Mercury planet from the above figure:

HtmlMap planetsMap = new HtmlMap();
planetsMap.NAME = "PlanetsMap";

MapArea mercuryArea = new MapArea();
mercuryArea.Shape = AreaShapesEnum.POLY;
mercuryArea.Href = 
  new Uri(@"");
mercuryArea.Alt = "Mercury";
mercuryArea.Coords = new List<int>(new int[] { 139, 58, 122, 66, 
116, 84, 121, 94, 124, 101, 132,
107, 140, 110, 146, 111, 138, 100, 134, 88, 134, 78, 137, 68 });

After all, add the generated HtmlMap object into the Form's Controls collection.


The HTML code of the map generated using the HtmlMap control is similar to the code written manually. But, with the HtmlMap control, we can produce maps even for automatically generated images (for some types of charts, geographical maps, etc.).


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


About the Author

Bashir Magomedov
Software Developer (Senior)
United Kingdom United Kingdom
Work: HSBC (
Regalia: PhD in CS, MCAD, MCPD: Web Developer, MCTS: .Net Framework 2.0., 3.5.
Interests: Programming, artificial intelligence, C#, .NET, HTML5, ASP.NET, SQL, LINQ.
Marital Status: Married, daughter

Comments and Discussions

GeneralMy vote of 5 Pin
Manoj Kumar Choubey17-Feb-12 20:53
professionalManoj Kumar Choubey17-Feb-12 20:53 
GeneralHotspot Pin
AQEEL AHMED4-Sep-08 14:15
memberAQEEL AHMED4-Sep-08 14:15 

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

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

Posted 12 Mar 2007


15 bookmarked