简体   繁体   English

linkbutton click事件

[英]linkbutton click event

I have dynamically added a linkbutton to the page. 我已经动态地向页面添加了一个链接按钮。 But the click event is not working. 但是click事件不起作用。 What could be the problem? 可能是什么问题呢? Thanks for your help. 谢谢你的帮助。 I am adding a linkbutton on button click event. 我在按钮点击事件上添加了一个链接按钮。 Here is my code. 这是我的代码。

protected void Button1_Click(object sender, EventArgs e)
{
    LinkButton lb = new LinkButton(); 
    lb.Text = "dsadsa";
    lb.ID = "22";
    lb.CommandArgument = "22"; 
    lb.CommandName = "22"; 
    lb.Command += new CommandEventHandler(lb1_Command); 
    PlaceHolder1.Controls.Add(lb);
}
protected void lb1_Command(object sender, CommandEventArgs e)
{
    Label1.Text = e.CommandName; 
}

the linkbutton isn't recreated when the linkbutton is clicked thus you're event handler isn't registered thus you're event doesn't get fired. 单击链接按钮时不会重新创建链接按钮,因此您没有注册事件处理程序,因此您的事件不会被触发。

Adding a button in an eventhandler is almost always a bad idea, you can add this by default on the page and just set it to Button.Visible = false . 在事件处理程序中添加按钮几乎总是一个坏主意,您可以在页面上默认添加它,并将其设置为Button.Visible = false This way you can register your eventhandler earlier in say Page_Load and set it visible from the event handler. 这样,您可以在Page_Load更早地注册您的事件处理程序,并从事件处理程序中将其设置为可见。

It's too late for adding the control on controls even handlers. 对控件甚至处理程序添加控件为时已晚。 The best way to add control is Init event, I guess on Load it will work too. 添加控件的最佳方法是Init事件,我想在Load它也会起作用。

您需要在页面加载中添加控件,因为单击后不会再次创建链接按钮。

请参阅linkbutton click和linkbutton命令: http//www.coderun.com/ide/? w = p-yDA -ntG0K4UrMkiImuRQ

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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