C#: calling a button event handler method without

2019-02-02 00:12发布

I have a button in my aspx file called btnTest. The .cs file has a function which is called when the button is clicked.

btnTest_Click(object sender, EventArgs e)

How can I call this function from within my code (i.e. without actually clicking the button)?

13条回答
放我归山
2楼-- · 2019-02-02 00:42

Use

btnTest_Click( this, new EventArgs() );
查看更多
爷、活的狠高调
3楼-- · 2019-02-02 00:44

You have to pass parameter sender and e to call button event handler in .cs file

btnTest_Click(sender,e);
查看更多
我只想做你的唯一
4楼-- · 2019-02-02 00:51

Simply call:

btnTest_Click(null, null);

Just make sure you aren't trying to use either of those params in the function.

查看更多
唯我独甜
5楼-- · 2019-02-02 00:52

If the method isn't using either sender or e you could call:

btnTest_Click(null, null);

What you probably should consider doing is extracting the code from within that method into its own method, which you could call from both the button click event handler, and any other places in code that the functionality is required.

查看更多
We Are One
6楼-- · 2019-02-02 00:52

It's just a method on your form, you can call it just like any other method. You just have to create an EventArgs object to pass to it, (and pass it the handle of the button as sender)

查看更多
Lonely孤独者°
7楼-- · 2019-02-02 00:52
btnSubmit_Click(btnSubmit,EventArgs.Empty);
查看更多
登录 后发表回答