ImageButton控件为图像按钮控件,它用于显示具体的图像,在功能上和Button控件相同。图1为ImageButton控件。
![]()
图1 ImageButton控件
(1)ImageButton控件常用属性
ImageButton控件常用属性及说明如表1所示。
表1 ImageButton控件常用属性及说明
|
属性 |
说明 |
|
ID |
控件ID |
|
AlternateText |
在图像无法显示时显示的替换文字 |
|
CausesValidation |
获取或设置一个值,该值指示在单击ImageButton控件时是否执行了验证 |
|
ImageUrl |
获取或设置在ImageButton控件中显示的图像的位置 |
|
Enabled |
获取或设置一个值,该值指示是否可以单击ImageButton以执行到服务器的回发 |
|
PostBackUrl |
获取或设置单击ImageButton控件时从当前页发送到的网页的URL |
ImageButton控件大部分属性设置同Button控件类似,下面主要介绍一下ImageUrl属性和AlternateText属性。
q ImageUrl属性
ImageUrl属性用于设置在ImageButton控件中显示图像的位置(URL)。在设置ImageUrl属性值时,可以使用相对URL,也可以使用绝对URL。相对URL使图像的位置与网页的位置相关联,当整个站点移动到服务器上的其他目录时,不需要修改ImageUrl属性值;而绝对URL使图像的位置与服务器上的完整路径相关联,当改更站点路径时,需要修改ImageUrl属性值。笔者建议,在设置ImageButton控件的ImageUrl属性值时,使用相对URL。
q AlternateText属性
使用此属性指定在ImageUrl属性中指定的图像不可用时显示的文本。
(2)ImageButton控件常用事件
ImageButton控件常用的事件是Click事件,该事件是在单击ImageButton控件时引发的事件。
下面示例主要通过设置ImageButton控件的ImageUrl属性和PostBackUrl属性来指定该控件的显示图片和超级链接页面。执行程序,示例运行结果如图2所示,单击图2上的【ImageButton】超级链接图片,页面链接到Default2.aspx上,运行结果如图3所示。

图2 ImageButton控件

图3 ImageButton控件链接页面
程序实现的主要步骤:
新建一个网站,默认主页为Default.aspx,然后再添加一个新页Default2.aspx,以便与Default.aspx进行链接,在Default.aspx页面上添加一个ImageButton控件,其属性设置如表2所示。
表2 ImageButton控件属性设置
|
属性名称 |
属性值 |
|
ID |
ImageButton1 |
|
AlternateText |
ImageButton按钮 |
|
BorderColor |
Black |
|
BorderWidth |
2px |
|
ImageUrl |
~/image/Image1.gif(图片的相对URL) |
|
PostBackUrl |
~/Default2.aspx(链接页面) |