日期:2014-05-20 浏览次数:20991 次
using System; using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (Request.Form["btn2"] != null) { Response.Write("S"); } Button btn1 = new Button(); btn1.Text = "button1"; this.form1.Controls.Add(btn1); btn1.Click += (o, ev) => { Button btn2 = new Button(); btn2.Text = "button2"; btn2.ID="btn2"; this.form1.Controls.Add(btn2); }; } }
------解决方案--------------------
private void button1_Click(object sender, EventArgs e) { Button newbtn = new Button(); newbtn.Name = "new"; newbtn.Text = "new"; newbtn.Size = new Size(100, 50); newbtn.Location = new Point(newbtn.Location.X+200,newbtn.Location.Y+100); newbtn.Visible = true; this.Controls.Add(newbtn); newbtn.Click += new EventHandler(newbtn_click); } private void newbtn_click(object sender, EventArgs e) { MessageBox.Show("success"); }
------解决方案--------------------
取消button注册的事件
private void button1_Click(object sender, EventArgs e) { button2.Click -= new EventHandler(button2_Click); //取消button2注册的事件 } private void button2_Click(object sender, EventArgs e) { MessageBox.Show("Hello"); } //先点击button2里会弹出"Hello"对话框的;若先点button1,再点button2就不会弹出"Hello"对话框了