如何在asp.net中删除动态创建的控件

Aar*_*kur 4 asp.net

我在面板中运行时创建了一些文件上传控件..现在我想删除点击链接按钮的控件.

我怎样才能做到这一点..

以下是动态控制箱子的代码..

protected void LinkButton1_Click(object sender, EventArgs e)
{
    Panel1.Visible = true;
    newattach(count);
    count++;      
}
private void newattach(int tot)
{
    int i;
    for (i = 0; i < tot; i++)
    {

        f1 = new FileUpload();
        f1.ID = "FileUpload" + count.ToString();
        f1.Height =20;
        f1.Width = 150;
        Panel1.Controls.Add(f1);
    }

}
Run Code Online (Sandbox Code Playgroud)

Yor*_*iev 8

如上所述,亚光?;)

 Panel1.Controls.Remove(Panel1.FindControl("FileUploadID"));
Run Code Online (Sandbox Code Playgroud)

应该工作,但是:

通过调试器运行整个页面,你会发现一些奇怪的东西......并且不了解.Net中的页面生命周期以及关于动态控件的基本原理,你可能会重新出现你的控件,这取决于页面生命周期中的时间你确实创建了动态控件,所以问题的答案更多的是如何以可控的方式创建正确的动态控件.所以:

asp.net中的动态控件 - Yuriy Solodkyy的那些原则适用于:

按照这种一致的方法动态创建控件:

  1. 在CreateChildControls方法中创建控件.
  2. 在LoadViewState中调用EnsureChildControls.
  3. 在Pair对象中包装和展开视图状态以强制调用LoadViewState.
  4. 在ViewState中支持的属性中保存页面动态部分的布局.
  5. 重新创建动态控件以响应事件处理程序中的用户操作.

其他重要说明:

  1. 在将控件添加到控件父控件的集合之前配置控件.
  2. 为动态创建的控件分配唯一ID.
  3. 在本地字段中保留对动态创建的控件的引用.
  4. 请记住,发布数据处理两次:在OnLoad之前和OnLoad之后.

我通常在页面模板后面使用以下代码:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;


public partial class template : BasePageGui
{


        #region Introduction
        private string msg; //this is the message you are going to show to your users


        private TextBox [ ] holderForTxt;     //holder for dynamic textboxes
        private DropDownList [ ] holderForDdl;
        private HtmlInputRadioButton [ ] holderForRdb;
        private HtmlInputCheckBox [ ] holderForCkb;

        private Label [ ] holderForAst;

        DataSet ds;             //dataset for metadata
        DataSet pds;            //parameter dataset
        DataSet rds;            //return set dataset


        #endregion


        #region Properties
        //set here page properties to use with the viewstate
        #endregion //Properties


        #region TemplateMethods
        protected override void OnInit ( EventArgs e )
        {   
        } //eof OnInit


        protected override void CreateChildControls ()
        {
                base.CreateChildControls ();
                CreateDynamicControls ();
        } 


        protected override object SaveViewState ()
        {
                return new Pair ( base.SaveViewState () , null );
        }

        protected override void LoadViewState ( object savedState )
        {
                base.LoadViewState ( ( (Pair) savedState ).First );
                EnsureChildControls ();
        } 


        protected void Page_Load ( object sender , EventArgs e )
        {   //comm -- the controls should be generated at the init stage and the databinding happens here

                if (this.IsPostBack == false)
                {
                } //eof on first load 
                else
                {
                } //eof on post back 
                this.DataBind ();
        } //eof method



        private void CreateDynamicControls ()
        {

        } //eof method

        #endregion //TemplateMethods


        #region DisplayMethods


        #endregion //DisplayMethods


        #region ClickEventHandlers

    #endregion ClickEventHandlers


        #region GridViewEventHanlders

    #endregion //GridViewEventHandlers
} //eof class 
Run Code Online (Sandbox Code Playgroud)