在另一个控件的click事件中执行一个控件的Click事件 [英] Execute Click event of one control within click event of another control

查看:76
本文介绍了在另一个控件的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..

推荐答案

而不是调用事件,为什么不创建方法?像这样

Instead of calling the event, why not create a method? Like this.

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.
}


或者您可以这样做.

Or you can do this.

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事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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