问题描述
我有一个类似的 onclick 事件:
I have a onclick event that is like:
public void OnMyButton_Click(object sender, EventArgs e)
如何在页面加载中调用它?
How can I call this from within pageload?
推荐答案
事件处理程序只是普通方法 - 您可以从定义它们的类中的任何位置调用它们 - 所以 Dan 的回答在技术上是正确的,尽管我觉得他遗漏了一些最佳实践建议.
Event handlers are just normal methods - you can call them from anywhere within the class they're defined in - so Dan's answer is technically correct, although I feel he leaves out a bit of best-practice advice.
与其从 load 事件调用事件处理程序,不如将事件处理程序中的代码移动到另一个方法中,然后从单击事件处理程序和页面加载方法中调用该方法.
Instead of calling your event handler from your load event, you should move the code in your event handler into another method, and then call that method from within both your click event handler, and your page load method.
这篇关于如何在页面加载时以编程方式调用我的 .net 按钮的 onclick?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!