ASP.NET 中ImageMap控件的用法
利用ASP.NETImageMap控件可以创建一个图像,使其包含许多可由用户单击的区域(热区),这些区域称为“作用点”。每一个作用点都可以是一个单独的超链接或回发事件。
常用属性:
HotSpotMode属性
HotSpotMode属性用于获取或设置单击热点区域后的默认行为方式。
ImageMap控件的HotSpotMode属性的枚举值如下表所示:
注意:HotSpotMode属性虽然为图片中所有热点区域定义了单击事件的默认行为方式,但在某些情况下,由于图片中热点区域的行为方式各不相同,所以还需要单独为每个热点区域定义HotSpotMode属性及其相关属性。
HotSpots属性
HotSpots属性用于获取HotSpots对象集合。
ImageMap控件由HotSpot类的实例组成。一个HotSpot定义图像映射中的一个可点击区域。ASP.NETFramework带有3种HotSpot类。
CircleHotSpot:用于在图像映射中定义一个圆形区域。
RectangleHotSpot:用于在图像映射中定义一个矩形区域。
PolygonHotSpot:用于在图像映射中定义一个不规则形状区域。
CircleHotSpot、RectangleHotSpot和PolygonHotSpot这3个子类的实例称为HotSpot对象。
示例代码:
Default.aspx
<%@PageLanguage="C#"AutoEventWireup="true"CodeFile="Default.aspx.cs"Inherits="_Default"%> <!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <htmlxmlns="http://www.w3.org/1999/xhtml"> <headrunat="server"> <title>示例8-4</title> <linkid="InstanceStyle"href="StyleSheet.css"type="text/css"rel="stylesheet"/> </head> <body> <formid="Form1"runat="server"> <div> <fieldsetstyle="width:290px"> <legendclass="mainTitle">ImageMap控件典型应用</legend> <br/> <asp:ImageMapID="ImageMap1"runat="server"ImageUrl="~/Image/pic1.png"OnClick="ImageMap1_Click"> <asp:RectangleHotSpotAlternateText="模块"Bottom="175"Left="77"NavigateUrl="http://localhost/" Right="150"Target="_blank"Top="119"/> <asp:CircleHotSpotAlternateText="处理1"HotSpotMode="PostBack"PostBackValue="Pro1" Radius="39"X="241"Y="50"/> <asp:CircleHotSpotAlternateText="处理2"HotSpotMode="PostBack"PostBackValue="Pro2" Radius="39"X="241"Y="285"/> <asp:PolygonHotSpotAlternateText="引擎"Coordinates="366,118,325,160,372,206,411,161" HotSpotMode="Inactive"/> </asp:ImageMap> <br/> <asp:LabelID="LabMessage"runat="server"></asp:Label> </fieldset> </div> </form> </body> </html>
Default.aspx.cs
usingSystem; usingSystem.Data; usingSystem.Configuration; usingSystem.Web; usingSystem.Web.Security; usingSystem.Web.UI; usingSystem.Web.UI.WebControls; usingSystem.Web.UI.WebControls.WebParts; usingSystem.Web.UI.HtmlControls; publicpartialclass_Default:System.Web.UI.Page { protectedvoidPage_Load(objectsender,EventArgse) { } protectedvoidImageMap1_Click(objectsender,ImageMapEventArgse) { Stringregion=""; switch(e.PostBackValue) { case"Pro1": region="处理1"; break; case"Pro2": region="处理2"; break; } LabMessage.Text="您单击的是<b>"+region+"</b>."; } }