Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I make one asp.net web forms app, I create user control, in this user control I insert few standard .net controls. Now in my page.aspx I want dynamicly to insert this user control. I put a button, something like "Add new control".
If I click on it I'm adding a new control to the page. And all this works fine.

I have a problem in removing of this controls. I write a function for removing. But first time when I click it doesn't remove the control, I have to click another time to remove the last control. I think is something with postback, don't know why at first post back it doesn't remove the control.

I'm addding programaticly controls to PlaceHolder which is inside UpdaPanel. Here is the c# code how i do this, pls anybody knows where i make mistake?

public partial class Clasess_And_Events : System.Web.UI.Page
{
    protected int NumberControlsForCategory
    {
        get { return (int)ViewState["NumControlsForCategory"]; }
        set { ViewState["NumControlsForCategory"] = value; }
    }
 

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            //Initiate the counter of dynamically added controls
            this.NumberControlsForCategory = 0;
            
        }
        else
        {
                //Controls must be repeatedly be created on postback
                this.createControlsCategory();
          
        }

 
           
    }

    private void createControlsCategory()
    {
        int count = this.NumberControlsForCategory;

        for (int i = 0; i < count; i++)
        {
             

            Control myUserControl = (Control)Page.LoadControl("UserControls/CategoryDropdown.ascx");
            myUserControl.ID = "CategoryControl_ID_" + i.ToString();

           
            PlaceHolder1.Controls.Add(myUserControl);
        }
    }
 

    

    protected void Add_Controll_Click(object sender, EventArgs e)
    {


        Control myUserControl = (Control)Page.LoadControl("UserControls/CategoryDropdown.ascx");
        myUserControl.ID = "CategoryControl_ID_" + NumberControlsForCategory.ToString();

         
        PlaceHolder1.Controls.Add(myUserControl);

        this.NumberControlsForCategory++;

        LinkButton10.Visible = true;
    }

  


    protected void Remove_Control_Click(object sender, EventArgs e)
    {
         
                Control myUserControl = (Control)Page.FindControl("CategoryControl_ID_" + NumberControlsForCategory.ToString());
                PlaceHolder1.Controls.Remove(myUserControl);
                this.NumberControlsForCategory--;
    }
}


What I have tried:

I make one asp.net web forms app, I create user control, in this user control I insert few standard .net controls. Now in my page.aspx I want dynamicly to insert this user control. I put a button, something like "Add new control". If I click on it I'm adding a new control to the page. And all this works fine.

I have a problem in removing of this controls.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900