C#调用按钮事件处理方法,而无需实际点击按钮 [英] C#: calling a button event handler method without actually clicking the button

查看:164
本文介绍了C#调用按钮事件处理方法,而无需实际点击按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在叫为btnTest aspx文件的按钮。该cs文件有一个按钮被点击时被调用函数。

  btnTest_Click(对象发件人,EventArgs的发送)

我怎样才能调用此函数从我的code范围内(即没有实际单击该按钮)?


解决方案

  btnTest_Click(NULL,NULL);

但该方法不使用这些参数的任一

(这是非常常见的不这样做。)

说实话,虽然这是恶心。如果你有code,它需要调用应遵循以下约定:

 保护无效btnTest_Click(对象发件人,EventArgs的发送)
{
   SomeSub();
}保护无效SomeOtherFunctionThatNeedsToCallThe code()
{
   SomeSub();
}保护无效SomeSub()
{
   // ...
}

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)?

解决方案

btnTest_Click(null, null);

Provided that the method isn't using either of these parameters (it's very common not to.)

To be honest though this is icky. If you have code that needs to be called you should follow the following convention:

protected void btnTest_Click(object sender, EventArgs e)
{
   SomeSub();
}

protected void SomeOtherFunctionThatNeedsToCallTheCode()
{
   SomeSub();
}

protected void SomeSub()
{
   // ...
}

这篇关于C#调用按钮事件处理方法,而无需实际点击按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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