2009-06-04 202 views
0

如何在我的自定義asp.net控件上創建一個可以冒泡到其父項的事件?我知道如何添加事件並處理它們,但是我遇到困難的地方在於如何添加一個可以通過中繼器控件連接到父級的事件。我如何爲自定義控件創建可綁定事件?

在中繼器中是控件,例如連接到自定義控件的onclick事件觸發,甚至觸發onclick事件提供的任何地址,就像任何其他控件一樣。我也很感激它,如果它在VB中,但C#也可以。

編輯

我四周尋找一個簡單的解決方案,跨越這裏面的作品,是很容易實現出來。有關更詳細的示例,請參閱http://msdn.microsoft.com/en-us/library/db0etb8x(VS.85).aspx

Public Event EditClick As EventHandler(Of MyEventArgs) 
Public Class MyEventArgs 
    Inherits EventArgs 
    Public ItemID As Int32 
End Class 

Protected Sub EditButton_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles EditButton.Click 
    Dim A As New MyEventArgs 
    A.ItemID = ItemID 
    RaiseEvent EditClick(Me, A) 
End Sub 

然後綁定事件。

<uc1:MyItem OnEditClick="EditItem" ... 

而後面的代碼處理事件

Sub EditItem(ByVal sender As Object, ByVal e As MyItem.MyEventArgs) 
     Edit(e.NewsItemID) 
End Sub 

回答

0

我四周尋找一個簡單的解決方案,跨越這裏面的作品,是很容易實現出來。有關更詳細的示例,請參閱http://msdn.microsoft.com/en-us/library/db0etb8x(VS.85).aspx

Public Event EditClick As EventHandler(Of MyEventArgs) 
Public Class MyEventArgs 
    Inherits EventArgs 
    Public ItemID As Int32 
End Class 

Protected Sub EditButton_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles EditButton.Click 
    Dim A As New MyEventArgs 
    A.ItemID = ItemID 
    RaiseEvent EditClick(Me, A) 
End Sub 

然後綁定事件。

<uc1:MyItem OnEditClick="EditItem" ... 

而後面的代碼處理事件

Sub EditItem(ByVal sender As Object, ByVal e As MyItem.MyEventArgs) 
     Edit(e.NewsItemID) 
End Sub 
1

看一看我的回答here。這是在幫你問什麼,

而且對事件使用的一些信息:

//declare the event using EventHandler<T> 
    public event EventHandler<ImeiRecordParserProblemEventArgs> ImeiRecordParserProblem; 

    //fire event: the check for null sees if the delegate is subscribed to 
    if (ImeiRecordParserProblem != null) 
    { 
    ImeiRecordParserProblem(this, new ImeiRecordParserProblemEventArgs(lineNumber + " : " + lex.Message,ProblemType.AmbiguousRecordType)); 
    } 

    //wire up the event in the catching code or otherwise assign in the .aspx 
    Irp.ImeiRecordParserProblem += new EventHandler<ImeiRecordParserProblemEventArgs>(Irp_ImeiRecordParserProblem); 

//and the EventArgs class: 
public class ImeiRecordParserProblemEventArgs : EventArgs 
    {}