Solved

Simulating a button click

Posted on 2009-07-03
5
298 Views
Last Modified: 2013-12-17
Hello,
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,
Thanks
0
Comment
Question by:Yurich
  • 2
  • 2
5 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 24776151
Not sure about WebForms, but in WinForms you use:

    Button1.PeformClick();
0
 
LVL 3

Accepted Solution

by:
khdani earned 500 total points
ID: 24776191
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
0
 
LVL 21

Author Closing Comment

by:Yurich
ID: 31599726
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.
0
 
LVL 21

Author Comment

by:Yurich
ID: 24807536
2Idle Mind: oh, and by the way, there is no PerformClick in web apps unfortunatelly ;)
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 24809110
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.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question