如何在 ASP.NET 后面的代码中为 ImageButton 连接 ClickI() 事件

问题描述:

我正在制作的自定义控件中有一个 ImageButton 实例变量.我对如何简单地设置它的点击事件的事件处理程序感到有些不安.来自 C#,我有点困惑,因为这是在 VB.NET 中.

I have a ImageButton instance variable in my custom control I am making. I am a little shaky on how to simply set up it's event handler for it's click event. Coming from C#, I am a little confused as this is in VB.NET.

截图:

Dim ctrlCloseImage As ImageButton = New ImageButton()
    With ctrlCloseImage
        .ID = "imgClose"
        .ImageUrl = "~/Web/Images/close.gif"
        .ToolTip = "Close"
        .CssClass = "popup_close_img"
        'This is where I left off
        AddHandler ctrlCloseImage.Click, AddressOf testSub(ctrlCloseImage, ??)
    End With
Me._ctrlCloseImage = ctrlCloseImage

然后:

Protected Sub testSub(ByVal Sender As Object, ByVal e As ImageClickEventArgs)
    Me.Page.Response.Write("Yay, you clicked the image button!")
End Sub

感谢您的帮助,如果我遗漏了任何重要的代码片段,请告诉我.同样,我希望它触发服务器端,我对在这里触发 javascript 函数不感兴趣.

Thanks for any help, and if I am missing anything important code-snip wise please let me know. Again, I am wanting this to fire server side, I am not interested in firing off a javascript function here.

你只需要添加委托:

AddHandler ctrlCloseImage.Click, AddressOf testSub 

你不能传递参数,因为这里没有触发事件.

You cannot pass arguments since the event is not triggered here.

但是,如果您不需要动态创建控件(通常这是不必要的),您可以在 aspx 上注册事件或使用 Handles 子句(与 C# 相反):

However, if you don't need to create a control dynamically (normally that's unnecessary) you could either register the event on the aspx or with the Handles clause(as opposed to C#):

Protected Sub testSub(ByVal Sender As Object, ByVal e As ImageClickEventArgs) _
    Handles ctrlCloseImage.Click
    Me.Page.Response.Write("Yay, you clicked the image button!")
End Sub