March 10, 2010  
  You are here:  Blog

QTCommunicationSimplified50.gif


Try Radicomm's Quicktalk, the first enterprise-class Push-To-Talk application for your Motorola/Symbol rugged devices, in your distribution center for free.

Click here to enter the 21st Century.


Raising a Button Click Event Programmatically
 
Location: BlogsNetSplore's Blog    
Posted by: Joe Rattz 12/27/2006 11:40:44 AM

Have you ever needed to raise a button-click event programmatically?  I sure have.  Sometimes you end up needing to programmatically simulate that a button was clicked by a user, but how do you accomplish this?  Often, I just call the event handling method that is registered for the button-click event.  And. most of the time this is acceptable.  Or, sometimes I put the actual code I need executed in a common method, and have the button-click event method call the common method, and then I can also call the common method from whereever I need.  However, there is a risk to these approaches.  What if the button-click event had more than one method registered?

If that question confuses you, you should be aware of the multicast delegate.  A multicast delegate allows more than one method to be registered with the delegate.  Button-click events are such a delegate.  This means it is possible to have more than one method get called when the user clicks a button.

For an example of why you might have more than one method mapped to an event, please read my blog post here.

The Problem

So, this takes us back to the original problem, how do you programmatically raise a button-click event?

The Solution

You can use the following C# code raise the event:

((IPostBackEventHandler)Button1).RaisePostBackEvent(null);

This code will raise the button-click event for Button1 just as though the user clicked the button.  Since we are passing a null as the EventArgs for the event, the registered methods must not attempt to do anything with it.  Or, you could create an EventArgs object and pass it.

There is at least one other approach using reflection, but I think the technique above is the cleaner way to do this.  However, if you are interested, you can read more about this topic here:

http://forums.asp.net/1/1509200/ShowThread.aspx

 

Copyright ©2006 Joe Rattz
Permalink |  Trackback

Comments (12)   Add Comment
Re: Raising a Button Click Event Programmatically    By Lisa on 10/3/2007 2:15:23 PM
I was having problems with this and now I want to Thank You, Thank You, Thank You, Thank You, Thank You!! This worked like charm.

Re: Raising a Button Click Event Programmatically    By ANON on 4/22/2008 11:39:46 AM
This only works for an ASP Webpage. It will not work for a MasterPage as an error is thrown around eventValidation (which you can get around by setting the Page directive to EnableEventValidation="False"...

You can use the more simple solution of:

Define your button OnClick Event, then call that routine directly

Protected Sub button1_OnClick(byval sender As Object, byval e As System.EventArgs) Handles Button1.Click

End Sub

To initiate the click simply do: Button1_Click(Button1, New System.EventArgs)



Re: Raising a Button Click Event Programmatically    By Joe Rattz on 4/22/2008 12:32:52 PM
You are correct about this causing problems if you are using event validation. I have turned that off for one of my applications because it causes other problems, such as false alarms caused by users interacting with a page's controls before the entire page is downloaded.

http://odetocode.com/Blogs/scott/archive/2007/04/13/10696.aspx

But, you are not correct about simply calling the method directly. This will only work if a single event method is registered for the button. If you have more than one method registered, as may be the case, then this will not work. If you read my post a little more closely, you will see that it is the case of when there is more than a single method added to the button that this article is referring to.

Thanks.

Re: Raising a Button Click Event Programmatically    By ANON on 4/23/2008 8:31:53 AM
Hey Joe..

My bad.. didn't really go through multiple methods bound to the same event..

The point I was trying to raise here was around EventValidation. The page directive option of EnableEventValidation="false" is only valid within an ASPX file.

If you use a Masterpage (MyFile.Master) the EnableEventValidation="false" is not valid, and therefore your solution doesn't work in that instance.

As a work around, although not as elegant, the only way I could achieve this was by initiating the OnClick event by calling the routine directly.

If you know of a better solution for this within a masterpage, please let me know.

TIA...

what about DropDownList    By Bruno Leclerc on 12/3/2008 6:01:10 AM
What about raising an SelectedIndexChanged in a dropdownlist ?
Programmatically SelectIndex=xxx change the selectedItem but seems to not raise the event ?

Re: what about DropDownList    By Bruno Leclerc on 12/3/2008 6:15:47 AM
I found this :
MyList.SelectedIndex=3;
((IPostBackDataHandler)MyList).RaisePostDataChangedEvent();
It Works

Re: Raising a Button Click Event Programmatically    By Joe Rattz on 12/3/2008 9:57:44 AM
Thanks Bruno!

Re: Raising a Button Click Event Programmatically    By John on 7/2/2009 11:49:53 PM
thanks for posting this solution

in window application    By Narendra Singh on 10/8/2009 8:04:37 AM
how can we raise a event Programmatically in c# window application.
actually i want to raise keypress event in another event
can u help me

Re: Raising a Button Click Event Programmatically    By Abhinav Kumar Tyagi on 10/19/2009 4:38:05 PM
hi dude..
i dont have experience in c#
but i feel, all the languages are interrelated and i can suggest you a solution in C++ MFC
--------------------------------------------------------------------------
button is a window and handle to it can be obtained by GetDlgItem if u are using a button dialog
then send WM_LBUTTONDOWN and WM_LBUTTONUP events to this button window using sendmessage function in c++;
---------------------------------------------------------------------------------
further queery u can mail me at abhityagi85@gmail.com

Re: Raising a Button Click Event Programmatically    By Kevin on 2/21/2010 2:03:44 AM
This is 100 % working for aspx pages

Re: Raising a Button Click Event Programmatically    By Jason on 3/4/2010 2:14:13 PM
This worked very well for me since I needed full replication of args for dynamic input types within GridView templates etc.

Thanks very much for the useful info.


Your name:
Title:
Comment:
Add Comment   Cancel 
 
Module Border Module Border
My Book

 

Module Border Module Border
Blog Archive
   
    

  Home|Freebies|Blog|Services|Articles|ASP.NET Depot|DotNetNuke Central|Contact Us
  Copyright 2005 Netsplore Terms Of Use Privacy Statement