如何在后面的代码中向ImageMap控件添加多个热点(以编程方式)

时间:2010-11-13 23:16:31

标签: c# asp.net controls imagemap

我的简单应用程序在图像上排列网页链接,并且取决于网络链接的图像数量类型是不同的。我使用ImageMap控件并在热点后面添加代码。坐标和网址来自数据库。下面是一个代码:

protected void ArrangeMapHotSpots(int voivodshipId)
{
    PolygonHotSpot hotSpot = new PolygonHotSpot();
    DataTable ImageMapDT = EzdrojeDB.ImageMapCoordinates(voivodshipId); // get data form DB   
    foreach (DataRow dr in ImageMapDT.Rows)
    {

        hotSpot.HotSpotMode = HotSpotMode.Navigate;
        hotSpot.AlternateText = "alt_text";
        hotSpot.Coordinates = dr["map_coord"].ToString();
        hotSpot.NavigateUrl = "~/resort.aspx?id=" + dr["id"].ToString();
        ImageMap1.HotSpots.Add(hotSpot);
    }
}

问题是我运行应用程序时只有一个链接处于活动状态(但此特定DataTable对象中有11行(链接))。

html代码:

<img id="ContentPlaceHolder1_ImageMap1" src="Images/VoivodMaps/dolnoslaskie.png" usemap="#ImageMapContentPlaceHolder1_ImageMap1" />

<map name="ImageMapContentPlaceHolder1_ImageMap1" id="ImageMapContentPlaceHolder1_ImageMap1"/>
area shape="poly" coords="303,427,444,447" href="resort.aspx?id=11" title="alt_text" alt="alt_text" />
area shape="poly" coords="303,427,444,447" href="resort.aspx?id=11" title="alt_text" alt="alt_text" />
area shape="poly" coords="303,427,444,447" href="resort.aspx?id=11" title="alt_text" alt="alt_text" />
area shape="poly" coords="303,427,444,447" href="resort.aspx?id=11" title="alt_text" alt="alt_text" />
area shape="poly" coords="303,427,444,447" href="resort.aspx?id=11" title="alt_text" alt="alt_text" />
area shape="poly" coords="303,427,444,447" href="resort.aspx?id=11" title="alt_text" alt="alt_text" />
area shape="poly" coords="303,427,444,447" href="resort.aspx?id=11" title="alt_text" alt="alt_text" />
area shape="poly" coords="303,427,444,447" href="resort.aspx?id=11" title="alt_text" alt="alt_text" />
area shape="poly" coords="303,427,444,447" href="resort.aspx?id=11" title="alt_text" alt="alt_text" />
area shape="poly" coords="303,427,444,447" href="resort.aspx?id=11" title="alt_text" alt="alt_text" />
area shape="poly" coords="303,427,444,447" href="resort.aspx?id=11" title="alt_text" alt="alt_text" />
</map>

已添加了11个热点,但每个热点在url字符串中具有相同的坐标和id。 任何人都可以向我解释发生了什么以及如何以编程方式向ImageMap添加多个热点。

1 个答案:

答案 0 :(得分:2)

您不断引用相同的PolygonHotSpot对象。您应该为循环的每次迭代创建一个新的。即尝试用以下内容替换您的功能:

protected void ArrangeMapHotSpots(int voivodshipId)
{
    PolygonHotSpot hotSpot;
    DataTable ImageMapDT = EzdrojeDB.ImageMapCoordinates(voivodshipId); // get data form DB   
    foreach (DataRow dr in ImageMapDT.Rows)
    {
        hotSpot = new PolygonHotSpot();
        hotSpot.HotSpotMode = HotSpotMode.Navigate;
        hotSpot.AlternateText = "alt_text";
        hotSpot.Coordinates = dr["map_coord"].ToString();
        hotSpot.NavigateUrl = "~/resort.aspx?id=" + dr["id"].ToString();
        ImageMap1.HotSpots.Add(hotSpot);
    }
}