本文介绍了在另一个控件的click事件中执行一个控件的Click事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

protected void Button1_Click(object sender, EventArgs e)
    {
        Button2_click()//Need to call Button2_click
    }



//================================================ ===========

Button1_click事件中,我希望调用Button_2 click事件.
可能吗?如果是,请提供代码..



//===========================================================

Within Button1_click event I wish to call Button_2 click event.
Is it possible? if yes please provide the code..

推荐答案

protected void Button1_Click(object sender, EventArgs e)
{
     RunThis();
}


protected void Button2_Click(object sender, EventArgs e)
{
     RunThis();
}


public void RunThis()
{
    //put the content of Button2_Click event here.
}


private void button1_Click(object sender, EventArgs e) {
    //option 1 of calling button2 click by simulating the click event
    button2.PerformClick();

    //option 2 of calling button2 click by calling the event itself
    button2_Click(sender, e);
}

private void button2_Click(object sender, EventArgs e) {
    MessageBox.Show("button2_Click");
}



选项3是修改您的代码实现,就像 walterhevedeich 建议的那样.



Option 3 is to modify your code implementation just like what walterhevedeich suggested.


这篇关于在另一个控件的click事件中执行一个控件的Click事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 13:55