private void GetSoftCheckList()
{
    int sortID = Int32.Parse(SoftSort.SelectedValue);
    DataTable table = (new SoftWareSystem()).GetSoftware(sortID);  //根据软件类别取得软件记录
   
    //释放PlaceHolder中的控件
    if (PlaceHolder.HasControls())
    {
PlaceHolder.Controls.Clear();
    }    //往PlaceHolder控件中添加软件设置控件
    for (int i = 0; i < table.Rows.Count; i++)
    {
Panel panel = new Panel();
panel.ID = "Panel" + i.ToString(); CheckBox chkSoft = new CheckBox();
chkSoft.ID = "CheckBox" + i.ToString();
chkSoft.Text = table.Rows[i]["软件名称"].ToString();
panel.Controls.Add(chkSoft);

CheckBox chkTryOut = new CheckBox();
chkTryOut.ID = "TryOut" + i.ToString();
chkTryOut.Text = "试用 ";
panel.Controls.Add(chkTryOut); Label lblStartDate = new Label();
lblStartDate.ID = "Label" + i.ToString();
lblStartDate.Text = "开始日期:";
panel.Controls.Add(lblStartDate); Chive.Web.UI.PopupCalendar StartDate = new Chive.Web.UI.PopupCalendar();
StartDate.ID = "StartDate" + i.ToString();
StartDate.Width = 100;
SetCalendarStyle(StartDate);
panel.Controls.Add(StartDate); Label lblEndDate = new Label();
lblEndDate.ID = "Label" + i.ToString();
lblEndDate.Text = "到期日期:";
panel.Controls.Add(lblEndDate); Chive.Web.UI.PopupCalendar EndDate = new Chive.Web.UI.PopupCalendar();
EndDate.ID = "EndDate" + i.ToString();
EndDate.Width = 100;
SetCalendarStyle(EndDate);
panel.Controls.Add(EndDate);
PlaceHolder.Controls.Add(panel);
    }
}在页面中有一个软件类别的DropDownList控件,当我点击DropDownList控件时,调用该过程进行动态更新,奇怪的是添加Panel控件和CheckBox控件语句并没有出错,而是提示“已存在多个Label0控件”。
请问:在ASP.net中运行时动态创建的控件要如何释放掉?

解决方案 »

  1.   

    if (PlaceHolder.HasControls())//把IF 去掉  让他每次都清理
        {
    PlaceHolder.Controls.Clear();
        }要是如此  还不行   还得跟踪看看
      

  2.   

    看的不是很明白,你自己的方法太多,可能你的问题出在细节上http://www.cnblogs.com/lovecherry/archive/2005/05/10/152455.html
    不知道对你的思路有没有帮助说说你是怎么做的,贴出你Pageload的代码
      

  3.   

    看看你的代码,有两个地方生成同样ID的label
    lblEndDate.ID = "Label" + i.ToString();
    lblStartDate.ID = "Label" + i.ToString();
    这样生成肯定会出现同名的控件,而这是不允许的
      

  4.   

    同意楼上的,在版面里边所有的ID应该是唯一值,不能重复
    lblEndDate.ID = "Label" + i.ToString();
    lblStartDate.ID = "Label" + i.ToString();
    你在选择改变时,应该全部清空,不用判断是否存在控件都可以.
      

  5.   

    lblEndDate.ID = "LabelEndDate" + i.ToString();
    lblStartDate.ID = "LabelStartDate" + i.ToString();