Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove dynamically created controls in asp.net

Tags:

asp.net

I have created some file upload control at run time in a panel..now i want to remove control on a click of link button.

how can i do this..

following is the code for crating control dynamically..

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);
    }

}
like image 693
Aarsh Thakur Avatar asked Feb 28 '23 17:02

Aarsh Thakur


1 Answers

As answered above by matt ?! ;)

 Panel1.Controls.Remove(Panel1.FindControl("FileUploadID"));

Should work, BUT:

Run you whole page trough the debugger you will find some strange things .... And without understanding page life cycle in .Net as well as basic princibles about dynamic controls you might get your control to reappear, depending on when in the page life cycle you do create your dynamic control, so the answer of the question is more about how to create properly dynamic controls in a controllable way. So:

Dynamic controls in asp.net - those principles by Yuriy Solodkyy apply:

Follow this consistent approach to creating controls dynamically:

  1. Create controls in the CreateChildControls method.
  2. Call EnsureChildControls in the LoadViewState.
  3. Wrap and unwrap view state in the Pair object to force calling LoadViewState.
  4. Save layout of the dynamic part of the page in properties backed in the ViewState.
  5. Recreate dynamic controls in response to user actions in event handlers.

Other Important Notes:

  1. Configure your controls before adding them to controls collection of the parent controls.
  2. Assign unique IDs to dynamically created controls.
  3. Keep references to dynamically created controls in local fields.
  4. Remember that post data is processed twice: before OnLoad and after OnLoad.

I generally do use the following code behind page template:

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 
like image 94
Yordan Georgiev Avatar answered Mar 14 '23 22:03

Yordan Georgiev