Monday 22 October 2012

Raising a Button Click Event Programmatically



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. 


No comments:

Post a Comment