Addhandler help

  • Thread starter Thread starter UGH
  • Start date Start date
U

UGH

I am adding image buttons dynamically and I need to add event handler when
the user clicks on one of those image buttons which will have different id
for reports.

Here is my code

LnkImage = New ImageButton()
LnkImage.ImageUrl = "~/printer.gif"
LnkImage.ID = "ib" & oArray(1, id)
AddHandler LnkImage.Command, AddressOf NewIbnCommandEvent()
cellItem.Controls.Add(LnkImage)

here is the handler

Private Sub NewIbnCommandEvent(ByVal sender As System.Object, ByVal e As
System.Web.UI.ImageClickEventArgs)
' Will do something here
Reponse.redirect(""~/appt.aspx?appt_id=" & LnkImage.ID")
End Sub

But I get this error message. Argument not specified for parameter 'e' of
'Private Sub NewIbnCommandEvent(sender As Object, e As
System.Web.UI.ImageClickEventArgs)'.

I do not know what to put in between the () AddHandler LnkImage.Command,
AddressOf NewIbnCommandEvent()

Thanks for any help.
 
Couple things,

first to get rid of the error, simply remove the () after the addressof, so
instead of:
AddHandler LnkImage.Command, AddressOf NewIbnCommandEvent()

do:
AddHandler LnkImage.Command, AddressOf NewIbnCommandEvent

simple, non?


Secondly, if you are going to use Command instead of Click (which you
probably want to in this case), your 2nd parameter needs to be of type
System.Web.UI.WEbControls.CommandEventArgs. ImageclickEventArgs is for the
click event


Lastly, consider using LnkImage.CommandArgument = "ib" & oArray(i,id) and
use e.commandArgument as your parameter in your redirect. This way LnkImage
doesn't have to be declared as a global property - simply scope it where you
create it and forget about it.

Karl
 
I otok out the () but it still gives me the error.

XXXXXXX.aspx.vb(218): Method 'Private Sub NewIbnCommandEvent()' does not
have the same signature as delegate 'Delegate Sub CommandEventHandler(sender
As Object, e As System.Web.UI.WebControls.CommandEventArgs)'.
 
If you read my original reply, you'll see that I addressed this...it's my
second point.

Karl
 
It is not clear to me but I figured out what you meant however the handler
that I added does not fired when I click on the button.
 
The linkimage must be added on postback and must have the handler hooked
then. In other words you CAN'T wrap the code in a if not page.ispostback.

Karl
 
Back
Top