2

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.

Snip:

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

And Then:

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

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.

4

3 回答 3

6

You just have to add the delegate:

AddHandler ctrlCloseImage.Click, AddressOf testSub 

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

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
于 2013-09-30T15:43:26.523 回答
0

You need to assign the click event a handler :

AddHandler ctrlCloseImage.Click, AddressOf testSub

This will assign the Click control the handler of address testSub

于 2013-09-30T15:43:58.357 回答
0

This should do the trick (just put the name of the method afyer AddressOf):

AddHandler ctrlCloseImage.Click, AddressOf testSub
于 2013-09-30T15:45:11.860 回答