問題描述
我有一個 OnClick=Button_Click 的按鈕.我想從另一個函數調用 Button_Click 但問題是我需要給它:
I have a button with OnClick=Button_Click. I want to call Button_Click from another function but the problem is that I need to give it:
(object sender, EventArgs e)
我應該為這些參數輸入什么?有什么辦法解決嗎?
What should I enter for those parameters? Is there any way around it?
推薦答案
你可以這樣做
Button_Click(null,EventArgs.Empty);
雖然我同意最好提取可以從任何地方調用的函數.
although I agree that it's better to extract function that could be called from anywhere.
例如,如果你有
protected void Button_Click(object sender, EventArgs e)
{
//some list of code
}
這段代碼應該放在一些新方法中,然后從 Button_Click 或任何其他方法中調用
this code should be put in some new method and then called from Button_Click or any other method
private void ExtractedMethod()
{
//some list of code
}
protected void Button_Click(object sender, EventArgs e)
{
ExtractedMethod();
}
我建議您閱讀Martin Fowler 所著的重構:改進現有代碼的設計一書.這是架子上的必備品.你會不時回到那本書,它是永恒的.
I recommend you to read a book Refactoring: Improving the Design of Existing Code by Martin Fowler. It's a must on a shelf. You will come back to that book from time to time, it's timeless.
這篇關于從函數調用按鈕 OnClick的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!