Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OnCommand vs OnClick

I was doing some research on my project (ASP.NET Web Forms) and came across 2 event types that seem similar, OnCommand and OnClick. I have read through the MSDN pages but I am still unclear on the differences between them.

What is the difference between OnCommand vs OnClick and when would I want to use which event?

//edited to specify for type of app

like image 569
kcubey Avatar asked May 02 '26 01:05

kcubey


1 Answers

If you find the documentation a bit unclear, that's understandable.

What happens is that OnClick is a lower-level event than OnCommand. You can think of OnClick as being more like a physical event, and OnCommand as being more like a logical event, so to speak.

When the control is clicked, it receives an OnClick event which tells the control that there has been a click. The control may do various things as a response. If the control is associated with a command, and if default processing of the event is allowed to take place, then the system will follow this with an OnCommand event.

(I am not sure / don't remember how much control you have over the suppression of default processing in WinForms, but you can certainly control that in the underlying Win32 programming layer.)

The OnCommand event may be triggered by means other than a click. For example, you may use a keyboard shortcut to activate a control, in which case there will be no OnClick.

The knowledge to take home from all this is the following:

Never use OnClick when OnCommand will suffice.

If you write your program to use OnClick for triggering Business Logic operations, then your program will require the user to use the mouse. There are many users who cannot use the mouse due to disability, there are many users who simply prefer the keyboard over the mouse, and there are many devices that do not even have a mouse.

(On those devices a click will often be simulated by a touch, but that's not guaranteed, and in any case all these clunky simulations should have never been necessary in the first place, the only reason they are added is because programmers were careless in the first place and they were doing silly things like triggering actions on OnClick instead of OnCommand.)

It would be okay to use OnClick if you were writing your own control. For example, if you were writing your own edit box control, and you wanted the user to be able to click somewhere within the control in order to place the caret at that exact location, then it would be okay to do that as a response to the OnClick event being triggered. But note how this action is for internal use of the control only, it does not trigger any action which is external to the control.

like image 52
Mike Nakis Avatar answered May 04 '26 14:05

Mike Nakis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!