如何处理放置在控件对象中的Button的单击事件?

时间:2013-09-20 13:10:36

标签: c# servercontrol

我有一个控制对象。在控制对象中,我添加了一个Button。我想处理Button对象的click事件。

      mapcomponent.MapObjectClick += new MapComponent.MapComponent.MapObjectEventHandler(mapcomponent_MapObjectClick);

    Public void mapcomponent_MapObjectClick(object sender, MapObjectEventArg e)
    {
        if (e != null)
        {
            var obj = sender as Control;
            var txt = obj.FindControl("txt1") as TextBox;
            if (txt != null)
                txt.Text = "hello";

            var btn = obj.FindControl("btn1") as Button;

            if (btn != null)
            {
                btn.Command += new CommandEventHandler(b_Command); //handler
            }
        }
    }

 //Not working
Public void b_Command(object sender, CommandEventArgs e)
    {
       //Want to call This Method
    }

2 个答案:

答案 0 :(得分:0)

你应该给你的按钮一个CommandName并在OnCommand事件上捕获它。

btn.Command += new CommandEventHandler(b_Command); //handler
btn.CommandName = "foo";

Public void b_Command(object sender, CommandEventArgs e)
{
    if(e.CommandName == "foo")
    {
        //Do stuff
    }
}

答案 1 :(得分:0)

我不完全确定你的观点是什么意思,但如果你想以编程方式调用Button click事件,你可以使用Button.PerformClick()方法。