想在ASP.NET页面实现:页面打开后台添加按钮A,点击A,自动添加按钮B,点击B自动添加按钮C。
我用btn.Click+=newSystem.EventHandler(btn_click);方式依次添加事件,A按钮在page_load中添加没问题,B按钮在点击A按钮...
我用btn.Click += new System.EventHandler(btn_click);方式依次添加事件,A按钮在page_load中添加没问题,B按钮在点击A按钮后添加没问题,现在关键是点击B按钮后不会添加C按钮,通过断点发现是B按钮的事件没执行,怎么才能让它执行?
protected void Page_Load(object sender, EventArgs e)
{
Button btn = new Button();
btn.ID = "A";
btn.Text = "A";
btn.Click += new System.EventHandler(btn_click);
this.form1.Controls.Add(btn);
}
private void btn_click(object sender, System.EventArgs e)
{
Button b1 = (Button)sender;
Button btn2 = new Button();
btn2.ID = "B" ;
btn2.Text = "B";
btn2.Click += new System.EventHandler(btn2_click);
this.form1.Controls.Add(btn2);
}
private void btn2_click(object sender, System.EventArgs e)
{
Button b2 = (Button)sender;
Button btn3 = new Button();
btn3.ID = "C" ;
btn3.Text = "C";
this.form1.Controls.Add(btn3);
} 展开
protected void Page_Load(object sender, EventArgs e)
{
Button btn = new Button();
btn.ID = "A";
btn.Text = "A";
btn.Click += new System.EventHandler(btn_click);
this.form1.Controls.Add(btn);
}
private void btn_click(object sender, System.EventArgs e)
{
Button b1 = (Button)sender;
Button btn2 = new Button();
btn2.ID = "B" ;
btn2.Text = "B";
btn2.Click += new System.EventHandler(btn2_click);
this.form1.Controls.Add(btn2);
}
private void btn2_click(object sender, System.EventArgs e)
{
Button b2 = (Button)sender;
Button btn3 = new Button();
btn3.ID = "C" ;
btn3.Text = "C";
this.form1.Controls.Add(btn3);
} 展开
1个回答
展开全部
因为点击B按钮后会回传,Page_Load又执行一次,btn2当然不见了,如果你确实要用这种方法实现这功能的话,可以在页面加一个HiddenField,然后按下面这样写:
protected void Page_Load(object sender, EventArgs e)
{
Button btn = new Button();
btn.ID = "A";
btn.Text = "A";
btn.Click += new System.EventHandler(btn_click);
this.form1.Controls.Add(btn);
if (HiddenField1.Value=="1")
{
Button btn2 = new Button();
btn2.ID = "B";
btn2.Text = "B";
btn2.Click += new System.EventHandler(btn2_click);
this.form1.Controls.Add(btn2);
}
}
private void btn_click(object sender, System.EventArgs e)
{
Button b1 = (Button)sender;
Button btn2 = new Button();
btn2.ID = "B";
btn2.Text = "B";
btn2.Click += new System.EventHandler(btn2_click);
this.form1.Controls.Add(btn2);
HiddenField1.Value = "1"; // 主要用来标记已经添加了按钮B
}
private void btn2_click(object sender, System.EventArgs e)
{
Button b2 = (Button)sender;
Button btn3 = new Button();
btn3.ID = "C";
btn3.Text = "C";
this.form1.Controls.Add(btn3);
}
protected void Page_Load(object sender, EventArgs e)
{
Button btn = new Button();
btn.ID = "A";
btn.Text = "A";
btn.Click += new System.EventHandler(btn_click);
this.form1.Controls.Add(btn);
if (HiddenField1.Value=="1")
{
Button btn2 = new Button();
btn2.ID = "B";
btn2.Text = "B";
btn2.Click += new System.EventHandler(btn2_click);
this.form1.Controls.Add(btn2);
}
}
private void btn_click(object sender, System.EventArgs e)
{
Button b1 = (Button)sender;
Button btn2 = new Button();
btn2.ID = "B";
btn2.Text = "B";
btn2.Click += new System.EventHandler(btn2_click);
this.form1.Controls.Add(btn2);
HiddenField1.Value = "1"; // 主要用来标记已经添加了按钮B
}
private void btn2_click(object sender, System.EventArgs e)
{
Button b2 = (Button)sender;
Button btn3 = new Button();
btn3.ID = "C";
btn3.Text = "C";
this.form1.Controls.Add(btn3);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询