Events and Delegates

M

Michael Morse

How in a asp.net web form can you have two different objects events call the
same sub with different signatures.

Example:
I have 1 ImageButton control and 1 LinkButton

Sub HandleTheEvent(ByVal sender as object, Byval e as eventArgs)
'blah
End Sub

By doing
AddHandler ImageButton1.Click, AddressOff Me.HandleTheEvent and
AddHandler LinkButton1.Click, AddressOff Me.HandleTheEvent

The signatures do not match up which is understandable. But what do I need
to do to make this work.

Thanks,

-mike
 
J

Jeffrey Tan[MSFT]

Hi Michael,

Thank you for posting in the community!

Based on my understanding, you have 2 web controls on the web form, 1 is a
normal button control, the other is an imagebutton. You want to use the
same event handler for these 2 buttons' click event.

=========================================
Actually, the Button's click event takes the normal EventHandler class,
while the ImageButton click event use the ImageClickEventHandler.
They take different EventArgs parameters. This is by the design of the C#
language. So you can not do like this.(I see you already understand this
point)

I think your 2 buttons(Button and ImageButton) may have the same click
handle logic, so you want to use the same event handler for them.

I think you can place the common click handle logic into a private method,
then use 2 different event handlers for each click event.
In both event handler, you can invoke this private method, then it will
behave the same as what you want.

Actually, for ImageButton's click event, .Net expose more feature for the
user to use, so it uses ImageClickEventHandler for its click event. This
event handler takes ImageClickEventArgs parameter, which has 2 more fields
"X" and "Y" to represent the coordinates where a user clicked on an
image-based ASP.NET server control.

That's why .Net expose 2 different event handlers for these 2 Buttons.

========================================
Please apply my suggestion above and let me know if it helps resolve your
problem.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.
Have a nice day!!

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
M

Michael Morse

Yeah that's what I have done in the meantime. I just wasn't sure I if I
could override the signature or do something that I just haven''t thought of
or new about.

Thanks

-mike
 
J

Jeffrey Tan[MSFT]

Hi Michael,

Thanks very much for your feedback.

I see your concern of original post :).

If you have any further questions, please feel free to post, I will help
you.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top