views:

240

answers:

2

How do I create an event on my custom asp.net control which can bubble up to its parent? I know how to add events and handle them but where I'm getting a bit stuck is on how to add an event which can be wired up to the parent in a repeater control for example.

In the repeater is the control, the onclick event for example connected to the custom control fires and the even fires to whatever address is provided in the onclick event just like any other control would do. I'd also appreciate it if it's in VB but c# will do as well.

EDIT

I was looking around for a simple solution and came across this which works and is very simple to implement. See http://msdn.microsoft.com/en-us/library/db0etb8x(VS.85).aspx for a more detailed example.

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

Then Bind the event.

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

And the code behind handle the event

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

Have a look at my answer here. This is doing what you ask,

Also some information on event usage:

    //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
    {}
CRice
A: 

I was looking around for a simple solution and came across this which works and is very simple to implement. See http://msdn.microsoft.com/en-us/library/db0etb8x%28VS.85%29.aspx for a more detailed example.

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

Then Bind the event.

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

And the code behind handle the event

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