Same method for different buttons

  • Thread starter Thread starter Mark Goldin
  • Start date Start date
M

Mark Goldin

I have a group of buttons.
When the user clicks on a button I'd like to execute
same code. Every button should send its caption to that method.
How can I do that?
 
Mark, I don't fully understand what you are trying to do. Each button has 1
method that can hook into it's Click event...multiple buttons can share the
same click event. Inside the click event, you can access all buttons. What
do you mean by "send its caption to that method"? As opposed to sending
information into a method, you should make the method get the information.
At worst case, you can recursively loop through the Page.Controls and all
child controls and check if the control IS a button.

Karl
 
I have a group of buttons.
When the user clicks on a button I'd like to execute
same code. Every button should send its caption to that method.
How can I do that?

Just assign all buttons the same Event Handler.

Or change the Handles keyword to handle all the buttons.click
 
Hi Mark,

You can have multiple events assigned to a common subroutine by listing the
events after the Handles clause

Private Sub MyButtonHandler _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click, _
Button2.Click, Button3.Click

Ken
Microsoft MVP [ASP.NET]
 
side note:
Instead of checking for the button's "caption" (text) you might want to the
the CommandName property instead. Its not necessarily any better or worse,
but its a little more in alignment with other similar scenarios (e.g.
handling events for templated controls)
 
Back
Top