简体   繁体   中英

How to handle a click event for Button that placed inside a Control Object?

I have a control Object . In control object I have added a Button . I want to handle click event for Button object.

      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
    }

You should give your button a CommandName and catch it on the OnCommand event.

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

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

我不确定您的方案的重点是什么,但是如果您要以编程方式调用Button单击事件,则可以使用Button.PerformClick()方法。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM