0

I created a web form with the following controls:

        enter image description here

Notice that there are 12 buttons, all of which fundamentally do the same thing. Is it possible to have one method (not twelve) that handles their OnClick event?

wjmolina
  • 2,625
  • 2
  • 26
  • 35
  • Yes it is possible. Create the event handler and let all the controls call that on `OnClick`. – Arian Motamedi Jul 08 '13 at 22:29
  • Of course, but there will be minimal changes in the code (+ vs -, etc.). How can I address those using `object sender` and `EventArgs e`? – wjmolina Jul 08 '13 at 22:32
  • Could use lambdas and pass in your own modifier: `mySubtractButton.OnClick += (sender, args) => HandleClick(sender, args, Operation.Subtract)`, or wire to a small wrapper method which does it: `myAddButton.Click += (sender, args) => Add(sender, args);` – Chris Sinclair Jul 08 '13 at 22:34
  • You should post the code for a couple of the current OnClick implementations. – Austin Salonen Jul 08 '13 at 22:38

1 Answers1

1

Yes, just in the object sender part of the event you parse the sender into a TextBox and get the id of the sender in order to know what to do.

mtzaldo
  • 105
  • 4