日期:2014-05-20  浏览次数:20472 次

|M| 如何动态添加和删除用户控件 谢谢在线以上结贴
index.aspx页面中有btn_add和btn_dll两个按钮
要求是点击btn_add的时候给index.aspx页添加一个news.ascx控件
继续点击继续添加news.ascx控件到index.aspx
点击btn_dll的时候删除最后添加的news.ascs控件,再点击再删除最后添加的那个

谢谢

------解决方案--------------------
private int ControlsCount { get { return ViewState[ "ControlsCount "] == null?0:(int)ViewState[ "ControlsCount "]; } set { ViewState[ "ControlsCount "] = value; } } private void Button1_Click(object sender, System.EventArgs e) { UserControl c = (UserControl) this.LoadControl( "MyControls.ascx "); this.Panel1.Controls.Add(c); this.ControlsCount ++; } private void Button2_Click(object sender, System.EventArgs e) { if(this.Panel1.Controls.Count > 0) { this.Panel1.Controls.RemoveAt(this.Panel1.Controls.Count-1); this.ControlsCount --; } } private void Page_Load(object sender, System.EventArgs e) { for(int i=0;i <this.ControlsCount;i++) { UserControl c = (UserControl) this.LoadControl( "MyControls.ascx "); this.Panel1.Controls.Add(c); } }
------解决方案--------------------
学习一下!
------解决方案--------------------
cpp2017(慕白兄) ,同意。不错