动态添加自定义控件:即按一下Botton添加一个UseControl,再按一下又添加一个
 
用ViewState记住你LoadControl几次

<asp:PlaceHolder id="ph" runat="server" />
int LoadedTimes
{
  get
  {
object o = ViewState["LoadedTimes"];
if (o == null)
return 0;
return (int)o;
  }
  set
  {
ViewState["LoadedTimes"] = value;
  }
}

void CreateControls()
{
  for (int i=0; i < LoadedTimes; i++)
  {
Control c = LoadControl("YourUserControl.ascx");
c.ID = "UC" + i.ToString();
ph.Controls.Add(c);
  }
}
void Page_Load(...)
{
CreateControls();
}

void Button_Click(...)
{
Control c = LoadControl("YourUserControl.ascx");
c.ID = "UC" + LoadedTimes.ToString();
ph.Controls.Add(c);
 
LoadedTimes++;
}
void GetValueButton_Click(..)
{
  for (int i=0; i < LoadedTimes; i++)
  {
YourControl uc = (YourControl)ph.FindControl(""UC" + i.ToString();");
//get the text from your user control
  }
}
 
如果你的稍稍改动少少就更完美了.....
void Button_Click(...)
{
Control c = LoadControl("YourUserControl.ascx");
c.ID = "UC" + LoadedTimes.ToString();
         //改为c.ID = "UC" + (LoadedTimes+1).ToString();
ph.Controls.Add(c);
 
LoadedTimes++;
}
posted on 2010-04-20 22:22  beibei11  阅读(738)  评论(0编辑  收藏  举报