0

我的简单应用程序在图像上排列网络链接,并且根据图像类型,网络链接的数量是不同的。我使用 ImageMap 控件并在热点后面添加代码。坐标和 url 是从数据库中获取的。下面有一段代码:

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 添加多个热点。

4

1 回答 1

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);
    }
}
于 2010-11-14T00:03:44.350 回答