当我按下按钮并为该控件定义一个Click事件处理程序时,我已经为按钮编写了一个代码隐藏( c#)来创建一个控件(ImageButton)。当我单击该控件时,如何才能强制它不消失并触发它的Click事件处理程序?我希望通过单击Button创建控件,因此不能像在其他答案中建议的那样在OnInit()方法中创建该控件。
码
protected void Button8_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
Control box = btn.Parent;
ImageButton lamp = new ImageButton();
lamp.ID = "MyLamp"; lamp.ImageUrl = "grey_lamp.jpg";
lamp.Style["Position"] = "absolute";
lamp.Style["Top"] = "40px";
lamp.Style["Left"] = "100px";
lamp.Style["Height"] = "20px";
lamp.Click += Image3_Click;
box.Controls.Add(lamp);
CreateNewTable();
base.OnInit(e);
}发布于 2018-01-22 01:04:56
下面是通过代码添加控件的示例
// the method; your button click method
private void ButtonClick() {
// create a new control
// my example would be a button
Button newButton = new Button();
// the button's content
newButton.Content = "Im a new button";
// the click event listener
newButton.Click += (_, __) => { // params: object sender, Clickeventargs
// here should be the instructions of the button to perform when clicked
Debug.Writeline("The new button has been clicked");
// do some amazing stuffs
}
// add the button to the parent control; a grid or stachpanel, etc.
someParentControl.Children.Add(newButton);
}https://stackoverflow.com/questions/48373017
复制相似问题