what is the difference between Control.OnClick and Control.InvokeOnClick - c#

i'm just wondering what the difference is between Control.OnClick and Control.InvokeOnClick methods.
the second thing i wonder ise, when it is asp.net they talk about 3 componenets: Click event, OnClick method, and button1_click eventhandler. but when it is desktop appcliatons they talk about only 2 componenets, Click event and its eventhandler. why is the onclik(invokeonclick) not mentioned at all?

OnClick allows you to add a handler for when the Click event occurs on the control.
InvokeOnClick will "Raise the Click event for the specified control".
So if you have assigned a handler to OnClick and then call InvokeOnClick on that control then your handler will be called.
Web is different. This answer may help you for invoking click on a 'web' button.

Related

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
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.
This is straight from the msdn page https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.oncommand.aspx
But trying to explain with little verbosity:
OnCommand is used to pass extra arguments to the handler. For example, if you have two buttons to sort list ascending and descending. Instead of writing separate OnClick handlers for each button , you can define single handler for both buttons and pass CommandName and CommandArgument
<asp:Button id="Button1"
Text="Sort Ascending"
CommandName="Sort"
CommandArgument="Ascending"
OnCommand="CommandBtn_Click"
runat="server"/>
<asp:Button id="Button2"
Text="Sort Descending"
CommandName="Sort"
CommandArgument="Descending"
OnCommand="CommandBtn_Click"
runat="server"/>
void CommandBtn_Click(Object sender, CommandEventArgs e)
{
switch(e.CommandName)
{
case "Sort":
// Call the method to sort the list.
Sort_List((String)e.CommandArgument);
break;
}
}
so here you have single handler where you can handle multiple buttons' clicks depending on what command argument buttons are passing.
Within the MSDN documentation it states in the remarks section the key difference.
With OnCommand:
The Command event is raised when the Button control is clicked. This event is commonly used when a command name, such as Sort, is associated with the Button control. This allows you to create multiple Button controls on a Web page and programmatically determine which Button control is clicked.
With OnClick:
The Click event is raised when the Button control is clicked. This event is commonly used when no command name is associated with the Button control (for instance, with a Submit button).
So, if you just want some code to execute when a button is clicked, but don't care about specific execution for specific cases then OnClick is just fine.
Edited to add: With OnCommand you could potentially have one handler that handles events from multiple buttons and executes the proper code based on the command name that's passed.

Invoke Event from Button Disable

Usually use this code for invoker the event from controller ex: button
button7.Click += new System.EventHandler(this.button7_Click);
or
button7.PerfomClick();
but if the button is disable not work and i need call the event even if disabled, it is possible?
PerformClick won't work for disable button. Instead of PerformClick, you can call the handler directly.
button7_Click(button7, EventArgs.Empty)
An event handler is nothing more than a function, so you could call it directly.

about ToolStripMenuItem

I'm having one ContextMenuStrip in that strip at runtime I'm adding one ToolStripMenuItem. And I added this ContextMenuStrip in the XtraGridView's MouseDown() event handler. And at the same time I've added the event handler for the newly inserted ToolStripMenuItem. And I have written one Event handler function for that ToolStripMenuItem. My problem of application is that when user right clicks on the XtraGridView it shows the required menu which I have added at runtime. And when I click on newly added ToolStripMenuItem it executes required event handler function but when I again do the same procedure the event handler function is executed for two times and so on...
Can anyone solve this problem?
Thanks.
You probably are subscribing to the Click event, in XtraGridView's click event. Each time the GridView's click event is raised causes you to subscribe to the click event handler again, so when user actually clicks on the ToolStripMenuItem all the handlers are called.
I suggest moving the subscription code to somewhere else.

Set up single event handler for multiple buttons in .NET?

An application I'm writting in silverlight/c# consists of 13 permanent buttons that when clicked perform a simple navigation to another page.
The problem is my code behind has 13 different event handlers(alot of code) for a nearly identical purpose.
Is there a way to detect which button was pressed so that a single event handler gets fired, and a simple if statement within could determine which page to go to?
yes: you can use the same method for all buttons, and use the parameter "sender" as "sender.Name" to get the name of the pressed button.
In the designer code of your program, tack on the same event handler for all 13 buttons (look for the code that has += and put the same event handler for all of them).
Notice that the event handler has an object (s) parameter. You can use this parameter as follows:
if(s.Name = "Button1") {//button 1 stuff}
else if (s.Name = "Button2") {button 2 stuff}
etc..
EDIT: should have been s.Name = "Button1, 2, 3, etc.."
Test the sender parameter of the button click event handler - you'll be able to test which button was the sender.
Use a Dictionary using 'sender' as key. The 'value' could be the page to navigate to.
If you have lots of code in your event handler you should break that out to a separate method anyway and send the button specific parameters to that method.
But you can still have one event handler if you look at the sender argument.

Can we Fire the button CLick event on Windowload In WPf C#

i want to FIre the button Click event When My Window is Loaded..
How Can i Achieve it in Wpf?
Create a single function with the shared behavior in your window, then call that function from both your loaded handler and your click handler.
As per this blog post in WinForms this was really easy by just calling PerformClick(), but in WPF you can do it with Automation, however as a commenter mentioned it's really easy if you have access to the button to just use RaiseEvent.
someButton.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
But as previously answered, if you only have a single handler that needs to be notified, then simply call that handler method directly.
You could use Automation to do it aswell - I've seen this suggested some places as the more flexible/robust method to use, but it seems a bit heavy weight to me compared to just calling the method you already have directly.
in you page_loaded event handler method, make a call to the click event like this:
_buttonName_click(sender, new RoutedEventArgs())
Trgger this event on Button whic u wanted to click
button.performclick();

Categories