Calling A Button OnClick from a function(从函数调用按钮 OnClick)
问题描述
我有一个 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从函数调用按钮 OnClick
基础教程推荐
- 全局 ASAX - 获取服务器名称 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
