Jordan Trajkov

Jordan Trajkov

  • 2k
  • 141
  • 7.2k

Add/remove user controls programaticaly to placeholder

Sep 20 2016 2:53 PM
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?
 
  1. public partial class Clasess_And_Events : System.Web.UI.Page  
  2. {  
  3.     protected int NumberControlsForCategory  
  4.     {  
  5.         get { return (int)ViewState["NumControlsForCategory"]; }  
  6.         set { ViewState["NumControlsForCategory"] = value; }  
  7.     }  
  8.    
  9.   
  10.     protected void Page_Load(object sender, EventArgs e)  
  11.     {  
  12.   
  13.         if (!IsPostBack)  
  14.         {  
  15.             //Initiate the counter of dynamically added controls  
  16.             this.NumberControlsForCategory = 0;  
  17.               
  18.         }  
  19.         else  
  20.         {  
  21.                 //Controls must be repeatedly be created on postback  
  22.                 this.createControlsCategory();  
  23.             
  24.         }  
  25.   
  26.    
  27.              
  28.     }  
  29.   
  30.     private void createControlsCategory()  
  31.     {  
  32.         int count = this.NumberControlsForCategory;  
  33.   
  34.         for (int i = 0; i < count; i++)  
  35.         {  
  36.                
  37.   
  38.             Control myUserControl = (Control)Page.LoadControl("UserControls/CategoryDropdown.ascx");  
  39.             myUserControl.ID = "CategoryControl_ID_" + i.ToString();  
  40.   
  41.              
  42.             PlaceHolder1.Controls.Add(myUserControl);  
  43.         }  
  44.     }  
  45.    
  46.   
  47.       
  48.   
  49.     protected void Add_Controll_Click(object sender, EventArgs e)  
  50.     {  
  51.   
  52.   
  53.         Control myUserControl = (Control)Page.LoadControl("UserControls/CategoryDropdown.ascx");  
  54.         myUserControl.ID = "CategoryControl_ID_" + NumberControlsForCategory.ToString();  
  55.   
  56.            
  57.         PlaceHolder1.Controls.Add(myUserControl);  
  58.   
  59.         this.NumberControlsForCategory++;  
  60.   
  61.         LinkButton10.Visible = true;  
  62.     }  
  63.   
  64.     
  65.   
  66.   
  67.     protected void Remove_Control_Click(object sender, EventArgs e)  
  68.     {  
  69.            
  70.                 Control myUserControl = (Control)Page.FindControl("CategoryControl_ID_" + NumberControlsForCategory.ToString());  
  71.                 PlaceHolder1.Controls.Remove(myUserControl);  
  72.                 this.NumberControlsForCategory--;  
  73.     }  
  74. }  
 
 

Answers (5)