讓您能夠處理使用者按一下影像的動作,它可以讓您擁有類似影像對應的功能。
<asp:ImageButtonid="ImageButton1" ImageUrl="string" Command="Command" CommandArgument="CommandArgument" CausesValidation="true | false" OnClick="OnClickMethod" runat="server"/>
備註
使用 ImageButton 控制項來顯示會回應滑鼠按鍵動作的影像。請設定 ImageUrl 屬性來指定要在控制項中顯示的影像。
按一下 ImageButton 控制項會同時引發 Click 和 Command 兩個事件。
您可以使用 OnClick 事件處理常式,以程式設計的方式來判斷按一下影像時的座標位置。然後依據座標值來撰寫回應的程式碼。請注意,原點 (0, 0) 是位於影像的左上角。
您可以使用 OnCommand 事件處理常式讓 ImageButton 控制項的行為像是個命令按鈕。使用 CommandName 屬性可以將命令名稱與控制項相關聯。這樣可以讓多個 ImageButton 控制項置於同一個 Web 網頁上。然後,就可以用程式設計的方式在 OnCommand 事件處理常式中辨識CommandName 屬性的值,決定每一個 ImageButton 控制項被按一下時所要執行的適當動作。CommandArgument 屬性也可以用來傳遞命令相關的額外資訊,例如指定遞增順序。
**注意 **因為 <asp:ImageButton> 項目沒有內容,所以您可以用 /> 來結束標記,以取代單獨的結束標記。
根據預設,按一下 ImageButton 控制項時便會執行網頁驗證。網頁驗證會判斷網頁上與驗證控制項關聯的輸入控制項,是否通過驗證控制項所指定的驗證規則。如果您的 ImageButton 控制項 (例如重設按鈕) 需要停用這種行為,請將 CausesValidation 屬性設定為 false。
如需 ImageButton Web 伺服器控制項之屬性和事件的詳細資訊,請參閱 ImageButton 類別文件。
範例
下列範例是示範如何使用 ImageButton 控制項來顯示滑鼠指標在影像上按一下時的座標。
<%@ Page Language="VB" AutoEventWireup="True" %>
<html>
<head>
<script runat="server">
Sub ImageButton_Click(sender As Object, e As ImageClickEventArgs)
Label1.Text="You clicked the ImageButton control at the " & _
"Coordinates: (" & e.X.ToString() & ", " & _
e.Y.ToString() & ")"
End Sub
</script>
</head>
<body>
<form runat="server">
<h3>ImageButton Sample</h3>
Click anywhere on the image.<br><br>
<asp:ImageButton id="imagebutton1" runat="server"
AlternateText="ImageButton 1"
ImageAlign="left"
ImageUrl="images\pict.jpg"
OnClick="ImageButton_Click"/>
<br><br>
<asp:label id="Label1" runat="server"/>
</form>
</body>
</html>
[C#]
<%@ Page Language="C#" AutoEventWireup="True" %>
<html>
<head>
<script runat="server">
void ImageButton_Click(object Source, ImageClickEventArgs e)
{
Label1.Text="You clicked the ImageButton control at the " +
"Coordinates: (" + e.X.ToString() + ", " +
e.Y.ToString() + ")";
}
</script>
</head>
<body>
<form runat="server">
<h3>ImageButton Sample</h3>
Click anywhere on the image.<br><br>
<asp:ImageButton id="imagebutton1"
AlternateText="ImageButton 1"
ImageAlign="left"
ImageUrl="images\pict.jpg"
OnClick="ImageButton_Click"
runat="server"/>
<br><br>
<asp:Label id="Label1"
runat="server"/>
</form>
</body>
</html>