Dynamically Removing Controls in a Parent Page from a Child Control

By | April 6, 2010

Yesterday I a colleague of mine asked me to dynamically remove a controls in a Parent Page from a Child Control without doing some code change on the Parent Page, this might be because he does not have access on the parent page codes and he want to do something with its controls given the fact that he already own a child control under that Parent Page, which mean you can only write your code changes from the Child Control.  Sounds confusing? Here is a sample screenshot of what I was trying to explain.

Control Sample

You have 3 sections there:


1. Parent Page – Which holds the Child Control.
2. Child Control – Which consists of 6 buttons.
3. PlaceHolder – Which have a button on it.

I tried a lot of options and will discuss the results of each.  So I have to declare all the variables I need first, all of this will happen on the ChildControl.

public Button myDynamicButton = new Button();
public UserControl myCustomControl = new UserControl();
public Button myOldButton = new Button();

First I tried to dynamically add and remove a normal .Net control like a text box – This wont solve his original problem

protected void btnAddControl_Click(object sender, EventArgs e)
 {
 PlaceHolder myPlaceHolder = (PlaceHolder)Page.FindControl("PlaceHolder1");

 myPlaceHolder.Controls.Add(myDynamicButton);
 myDynamicButton.Text = "Hello World";
 }

 protected void btnRemoveControl_Click(object sender, EventArgs e)
 {
 PlaceHolder myPlaceHolder = (PlaceHolder)Page.FindControl("PlaceHolder1");
 if (myPlaceHolder.Controls.Contains(myDynamicButton))
 {

 myPlaceHolder.Controls.Remove(myButtonToRemove);
 myButtonToRemove.Dispose();
 }
 }

Second I tried to dynamically add and remove a custom control –  This will demonstrate adding and removing Custom Controls you might have created

protected void btnAddControl_Click(object sender, EventArgs e)
 {
 myCustomControl = (UserControl)Page.LoadControl("SampleControlToLoad.ascx");
 PlaceHolder myPlaceHolder = (PlaceHolder)Page.FindControl("PlaceHolder1");

 myPlaceHolder.Controls.Add(myCustomControl);
 }
 protected void btnRemoveControl_Click(object sender, EventArgs e)
 {
 PlaceHolder myPlaceHolder = (PlaceHolder)Page.FindControl("PlaceHolder1");
 if (myPlaceHolder.Controls.Contains(myCustomControl))
 {
 myPlaceHolder.Controls.Remove(myCustomControl);
 myDynamicButton.Dispose();
 }
 }

Third I tried to remove a control that is already exisiting on the Parent Page and add it back with a different property (This is what he actually wanted)

protected void btnAddControl_Click(object sender, EventArgs e)
 {
 PlaceHolder myPlaceHolder = (PlaceHolder)Page.FindControl("PlaceHolder1");
 if (myPlaceHolder.Controls.Contains(myOldButton))
 {
 //This can also be used
 //myOldButton.Parent.Controls.Add(myOldButton); 

 myPlaceHolder.Controls.Add(myOldButton);
 }
 }
 protected void btnRemoveControl_Click(object sender, EventArgs e)
 {
 PlaceHolder myPlaceHolder = (PlaceHolder)Page.FindControl("PlaceHolder1");
 myOldButton = (Button)myPlaceHolder.FindControl("myButton");
 if (myOldButton != null)
 {   
 //This can also be used
 //myOldButton.Parent.Controls.Remove(myOldButton);

 myPlaceHolder.Controls.Remove(myOldButton);
 myOldButton.Dispose();
 }

 }


Leave a Reply