如何为自定义控件创建可绑定事件?

时间:2022-09-02 13:37:07

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.

如何在我的自定义asp.net控件上创建一个可以冒泡到其父级的事件?我知道如何添加事件并处理它们但我遇到的问题是如何添加一个事件,例如可以在转发器控件中连接到父节点。

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.

在转发器中是控件,onclick事件例如连接到自定义控件触发,偶数触发到onclick事件中提供的任何地址,就像任何其他控件一样。我也很感激它,如果它在VB中,但c#也会这样做。

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.

我正在寻找一个简单的解决方案,并且遇到了这个有效并且实现起来非常简单的方法。有关更详细的示例,请参阅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

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

2 个解决方案

#1


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
    {}

#2


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.

我正在寻找一个简单的解决方案,并且遇到了这个有效并且实现起来非常简单的方法。有关更详细的示例,请参阅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

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

#1


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
    {}

#2


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.

我正在寻找一个简单的解决方案,并且遇到了这个有效并且实现起来非常简单的方法。有关更详细的示例,请参阅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

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