using System;using System.Collections.Generic;using System.linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Diagnostics;public partial class testingSandBox : System.Web.UI.Page{protected voID Page_Load(object sender,EventArgs e){ boot();}public voID boot(){ firstFunc();}public voID firstFunc(){ DeBUG.Writeline("func1"); button btn1 = new button(); btn1.Text = "btn1"; btn1.ID = "btn1"; btn1.Click += new EventHandler(secFunc); form1.Controls.Add(btn1);}public voID secFunc(object sender,EventArgs e){ DeBUG.Writeline("func2"); button btn2 = new button(); btn2.Text = "btn2"; btn2.ID = "b2"; btn2.Click += new EventHandler(thirdFunc); form1.Controls.Add(btn2); button btn1 = (button)this.FindControl("btn1"); //btn1.Click-=new EventHandler(secFunc);}public voID thirdFunc(object sender,EventArgs e){ DeBUG.Writeline("func3"); button btn3 = new button(); btn3.Text = "btn3"; btn3.ID = "b3"; btn3.Click += new EventHandler(fourthFunc); form1.Controls.Add(btn3);}public voID fourthFunc(object sender,EventArgs e){ DeBUG.Writeline("func4"); button btn4 = new button(); btn4.Text = "btn4"; form1.Controls.Add(btn4);}
}
解决方法 ASP.NET在每个请求上重新创建ENTIRE控制树.您在.ascx文件中编写的内容将转换为C#代码文件(您可以在ASP.NET Temporary files文件夹中找到它们),该文件将创建控件,并且该代码将在每个请求上运行.但在你的情况下,会发生这种情况:Request 1: You start out with button1.Request 2: You start out with button1. A click event for it is received and processed. In the event handler you add button2. You end up with button1 and button2.Request 3: You start out with button1. A click event for button2 is received. Unfortunately there is no button2,since the control tree got recreated. The event is ignored. You end up with just button1.
ASP.NET webforms中的动态控件很难.您需要手动跟踪添加的控件,并在每个后续请求的开头重新创建它们. ASP.NET不记得这个给你.
总结以上是内存溢出为你收集整理的c#events:第二级后没有开火全部内容,希望文章能够帮你解决c#events:第二级后没有开火所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)