且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C#:调用按钮事件处理程序方法而不实际单击按钮

更新时间:2023-12-06 12:20:16

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()
{
   // ...
}