Simulating a button click

What is a correct way of programmatically simulating a button click?

I know that I can do this:

this.Button1_Click( null, null );

but I have a feeling that there is another better way of doing it, but I can't remember if there actually is.

Would appreciate your help,
LVL 21
Who is Participating?
khdaniConnect With a Mentor Commented:
it's not a good thing simulating that way a button click, it's better to put all your "click" code in some function which you call in button click handler, and then you can call this function also from other places in code.

anyway, you can do this
this.Button1_Click(this, new EventArgs()); - it's good as long as you don't need the EventArgs object
Mike TomlinsonMiddle School Assistant TeacherCommented:
Not sure about WebForms, but in WinForms you use:

YurichAuthor Commented:
Thanks, I did end up doing a function and calling it from a button click and from another method. Just initially it was simpler to initiate a button click.
YurichAuthor Commented:
2Idle Mind: oh, and by the way, there is no PerformClick in web apps unfortunatelly ;)
Mike TomlinsonMiddle School Assistant TeacherCommented:
Gotcha...I wasn't sure...which is why I put the disclaimer on it.  I'm a WinForms guy!  =)

Glad you've got a working solution.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.