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.

Karl Anderson
  • 34,606
  • 12
  • 65
  • 80
Jason Renaldo
  • 2,802
  • 3
  • 37
  • 48
  • You may want to convert your vb.net code to c# code. I would.. http://stackoverflow.com/questions/88359/what-is-the-best-c-sharp-to-vb-net-converter – mehmet mecek Sep 30 '13 at 15:46

3 Answers3

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
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
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

Nunners
  • 3,047
  • 13
  • 17
0

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

AddHandler ctrlCloseImage.Click, AddressOf testSub
Superzadeh
  • 1,106
  • 7
  • 23