imagemap控件是一个让你可以在图片上定义热点(hotspot)区域的服务器控件。用户可以通过点击这些热点区域进行回发(postback)操作或者定向(navigate)到某个url位址。该控件一般用在需要对某张图片的局部范围进行互动操作时,其主要属性有hotspotmode、hotspots和主要操作click。
? hotspotmode:顾名思义为热点模式,对应枚举类型system.web.ui.webcontrols.hotspotmode。其选项及说明如下:
1) notset:未设置项。虽然名为未设置,但其实默认情况下会执行定向操作,定向到你指定的url位址去。如果你未指定url位址,那默认将定向到自己的web应用程序根目录。
2) navigate:定向操作项。定向到指定的url位址去。如果你未指定url位址,那默认将定向到自己的web应用程序根目录。
3) postback:回发操作项。点击热点区域后,将执行后部的click事件。
4) inactive:无任何操作,即此时形同一张没有热点区域的普通图片。
? hotspots:该属性对应着system.web.ui.webcontrols.hotspot对象集合。hotspot类是一个抽象类,它之下有circlehotspot(圆形热区)、rectanglehotspot(方形热区)和polygonhotspot(多边形热区)三个子类。实际应用中,都可以使用上面三种类型来定制图片的热点区域。如果需要使用到自定义的热点区域类型时,该类型必须继承hotspot抽象类。下面即有个自定义的菱形热区diamondhotspot范例可以参考。
? click:对热点区域的点击操作。通常在hotspotmode为postback时用到。
对imagemap控件有了以上一个基本了解后,接着看asp.net quickstart提供个两个应用示例和最后一个自定义菱形热区示例就会有所体会了。
示例一:imagemap 多种hotspotmode 示例
<%@ page language="c#" %>
<!doctype html public "-//w3c//dtd xhtml 1.1//en" "http://www.w3.org/tr/xhtml11/dtd/xhtml11.dtd">
<script runat="server">
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>untitled page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h3><font face="verdana">imagemap 多种 hotspotmode 示例</font></h3>
<asp:imagemap id="buttons" imageurl="hotspot.jpg" alternatetext="navigate buttons"
runat="server">
<asp:rectanglehotspot
hotspotmode="navigate"
navigateurl="navigate1.htm"
alternatetext="button 1"
top="30"
left="175"
bottom="110"
right="355">
</asp:rectanglehotspot>
<asp:rectanglehotspot
hotspotmode="navigate"
navigateurl="navigate2.htm"
alternatetext="button 2"
top="155"
left="175"
bottom="240"
right="355">
</asp:rectanglehotspot>
<asp:rectanglehotspot
hotspotmode="navigate"
navigateurl="navigate3.htm"
alternatetext="button 3"
top="285"
left="175"
bottom="365"
right="355">
</asp:rectanglehotspot>
</asp:imagemap>
</div>
</form>
</body>
</html>
示例二:imagemap postback 模型示例
<%@ page language="c#" %>
<!doctype html public "-//w3c//dtd xhtml 1.1//en" "http://www.w3.org/tr/xhtml11/dtd/xhtml11.dtd">
<script runat="server">
void buttons_clicked(object sender, imagemapeventargs e)
{
label1.text = e.postbackvalue + " clicked!";
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>untitled page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h3>
<font face="verdana">imagemap postback 模型示例</font></h3>
<asp:imagemap id="buttons" imageurl="hotspot.jpg" alternatetext="navigate buttons"
hotspotmode="postback" runat="server">
<asp:rectanglehotspot
hotspotmode="postback"
postbackvalue="button1"
alternatetext="button 1"
top="30"
left="175"
bottom="110"
right="355">
</asp:rectanglehotspot>
<asp:rectanglehotspot
hotspotmode="postback"
postbackvalue="button2"
alternatetext="button 2"
top="155"
left="175"
bottom="240"
right="355">
</asp:rectanglehotspot>
<asp:rectanglehotspot
hotspotmode="postback"
postbackvalue="button3"
alternatetext="button 3"
top="285"
left="175"
bottom="365"
right="355">
</asp:rectanglehotspot>
<asp:rectanglehotspot
hotspotmode="postback"
postbackvalue="background"
alternatetext="background"
top="0"
left="0"
bottom="390"
right="540">
</asp:rectanglehotspot>
</asp:imagemap>
<p>
<h3>
<font face="verdana">
<asp:label id="label1" runat="server"></asp:label>
</font>
</h3>
</p>
</div>
</form>
</body>
</html>
示例三:自定义热点区域 diamondhotspot 菱形热点区域
1. 创建diamondhotspot类
using system;
using system.data;
using system.configuration;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;
namespace hotspottest
{
public class diamondhotspot : system.web.ui.webcontrols.hotspot
{
public int centerx
{
get
{
object val = viewstate["centerx"];
if (val == null)
return 0;
else
return (int)val;
}
set
{
viewstate["centerx"] = value;
}
}
public int centery
{
get
{
object val = viewstate["centery"];
if (val == null)
return 0;
else
return (int)val;
}
set
{
viewstate["centery"] = value;
}
}
public int width
{
get
{
object val = viewstate["width"];
if (val == null)
return 0;
else
return (int)val;
}
set
{
viewstate["width"] = value;
}
}
public int height
{
get
{
object val = viewstate["height"];
if (val == null)
return 0;
else
return (int)val;
}
set
{
viewstate["height"] = value;
}
}
protected override string markupname
{
get
{
return "poly";
}
}
public override string getcoordinates()
{
return centerx.tostring() + "," +
(centery - height / 2).tostring() + "," +
(centerx + width / 2).tostring() + "," +
centery.tostring() + "," +
centerx.tostring() + "," +
(centery + height / 2).tostring() + "," +
(centerx - width / 2).tostring() + "," +
centery.tostring();
}
}
}
2. 在页面写 register 指令
<%@ register tagprefix="hotspottest" namespace="hotspottest" %>
3. 在后部代码里动态添加菱形热区,或者直接在页面代码里声明菱形热区
? 后部代码动态添加:
protected void page_load(object sender, eventargs e)
{
if (!ispostback)
{
hotspottest.diamondhotspot dhs = new hotspottest.diamondhotspot();
dhs.centerx = 100;
dhs.centery = 50;
dhs.height = 100;
dhs.width = 50;
imagemap1.hotspots.add(dhs);
}
}
? 页面代码声明:
<asp:imagemap id="imagemap1" runat="server" imageurl="hotspot.jpg" hotspotmode="postback">
<hotspottest:diamondhotspot centerx="100" centery="50" height="100" width="50" />
</asp:imagemap>
[总结]:在大部分web应用中可能很少会用到imagemap,但正因为有了imagemap,才让web应用更加多姿多彩。有了imagemap,我们可以动态的在一张图片中的某个局部范围内进行相应的处理请求的梦想再也不遥远。
商业源码热门下载www.html.org.cn