如何在页面加载中以编程方式调用.net按钮的onclick? [英] How can I programmatically call my .net button's onclick in page load?

查看:55
本文介绍了如何在页面加载中以编程方式调用.net按钮的onclick?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似的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.

您应该将事件处理程序中的代码移到另一个方法中,然后再从click事件处理程序和页面加载方法中调用该方法,而不是从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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆