ImageButton.OnClick(ImageClickEventArgs) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
protected:
virtual void OnClick(System::Web::UI::ImageClickEventArgs ^ e);
protected virtual void OnClick (System.Web.UI.ImageClickEventArgs e);
abstract member OnClick : System.Web.UI.ImageClickEventArgs -> unit
override this.OnClick : System.Web.UI.ImageClickEventArgs -> unit
Protected Overridable Sub OnClick (e As ImageClickEventArgs)
參數
ImageClickEventArgs,其中包含事件資料。
範例
下列範例示範如何指定和撰寫事件處理常式的程式 Click 代碼,以顯示使用者按一下影像的座標。
注意
下列程式碼範例會使用單一檔案程式碼模型,如果直接複製到程式碼後置檔案,可能無法正常運作。 此程式碼範例必須複製到副檔名為 .aspx 的空白文字檔中。 如需Web Form程式碼模型的詳細資訊,請參閱ASP.NET Web Forms頁碼模型。
<%@ Page Language="C#" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>ImageButton Sample</title>
<script language="C#" runat="server">
void ImageButton_Click(object sender, ImageClickEventArgs e)
{
Label1.Text = "You clicked the ImageButton control at the coordinates: (" +
e.X.ToString() + ", " + e.Y.ToString() + ")";
}
</script>
</head>
<body>
<form id="form1" 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>
<%@ Page Language="VB" AutoEventWireup="True" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>ImageButton Sample</title>
<script language="VB" 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 id="form1" 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>
備註
Click按一下控制項時 ImageButton ,就會引發 事件。 您可以使用 OnClick 事件處理常式,以程式設計方式判斷按一下影像的座標。 然後,您可以根據這些座標的值來撰寫回應的程式碼。 請注意,原點 (0,0) 位於影像的左上角。
引發事件會透過委派叫用此事件處理常式。 如需詳細資訊,請參閱如何:在Web Form應用程式中取用事件。
OnClick 方法也允許衍生類別處理事件,而不用附加委派。 這是在衍生類別中處理事件的慣用技巧。
給繼承者的注意事項
當在衍生類別中覆寫 OnClick(ImageClickEventArgs) 時,請確定呼叫基底類別的 OnClick(ImageClickEventArgs) 方法,使已註冊的委派能接收到事件。
適用於
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應