2012-09-06 76 views
3

我正在構建使用ASP和C#的網站,我想知道是否有可能將自定義事件添加到asp控件。將自定義事件添加到ASP控件

我想添加一個OnClick,OnMouseDown,OnMouseUp(etc.)事件到<asp:image>

可能嗎?

在此先感謝

回答

4

是的,你可以這樣做。

HTML

<asp:Image runat="server" ID="Image1" /> 

後面的代碼

protected void Page_Load(object sender, EventArgs e){ 

     this.Image1.Attributes.Add("onmouseup", "alert('this is the OnMouseUp event')"); 

    } 

編輯

It can be do it using DHTML but as you may know 
    DHTML is the art of combining HTML, JavaScript, DOM, and CSS. 

    protected void Page_Load(object sender, EventArgs e){ 
     //change the Image Url when the click event fire 
     this.Image1.Attributes.Add("onclick", "this.src='image1.jpg'"); 

    } 
+0

有什麼辦法可以在沒有JavaScript的情況下做到嗎? – lsxliron

+0

@lsxliron是的,您可以使用DHTML將事件添加到HTML元素並對此事件做出反應。 –

0

注意的鼠標事件可能是在服務器上處理不切實際,最好在客戶端進行本地處理(更快),所以網絡速度對處理事件的速度沒有影響。

相關問題