2010-07-19 175 views
1

我正在爲我的Web應用程序創建一個ASP.NET自定義控件,它基本上是一個包含在多個<div>元素(用於樣式目的)內的ASP.NET按鈕控件。單擊事件處理程序與自定義控件按鈕

如何爲我的自定義控件創建Click事件處理程序,以便我的控件充當ASP.NET按鈕?

即,

<cc:Button id="myButton" runat="server" Text="Submit" /> 

Sub myButton_Click(sender as object, e as EventArgs) Handles myButton.Click 

End Sub 

回答

4

在後面的代碼,在你的類聲明的事件:當您處理的constinuent按鈕的Click事件然後

Public Event Click(sender as object, e as EventArgs) 

,引發該事件

Sub btnButton_Click(sender as object, e as EventArgs) Handles btnButton.Click 
    RaiseEvent Me.Click(Me, EventArgs) 
End Sub 
+0

不得不做一個小小的改變,但除此之外,完美的工作! Sub btnButton_Click(sender as object,e as EventArgs)Handle btnButton.Click RaiseEvent Click(Me,e) End Sub – Curt 2010-07-20 09:45:18

0

大片段,對於用戶控制來說非常方便,以便在需要通知父屏幕的情況下引發事件。我的通用形式是這樣的:

Public Event eventTrigger(ByVal sender As Object, ByVal e As EventArgs) 

Private Sub ucUserControl_Changed(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtText1.TextChanged, txtText2.TextChanged,txtetc.TextChanged 

    RaiseEvent eventTrigger(Me, e) 

End Sub 
+0

這應該是一個評論,而不是回答。檢查這個[metaSO問題](http://meta.stackexchange.com/questions/7656/how-do-i-write-a-good-answer-to-a-question)和[Jon Skeet:Coding Blog]( http://msmvps.com/blogs/jon_skeet/archive/2009/02/17/answering-technical-questions-helpfully.aspx)如何給出正確的答案。 – Yaroslav 2012-10-11 12:09:18

相關問題