Trying to remove controls dynamically from ASP.NET page.

Posted on 2011-02-16
Last Modified: 2012-05-11

 I am have trouble in deleting controls from the page. I just want to remove a control dynamically from the page. The type of code I am using is :

        tb1 = DirectCast(tblTable.FindControl("Ctrl999"), TextBox)
        tb2 = DirectCast(tblTable.FindControl("Ctrl999"), TextBox)

It seems to find the control, but does not seem to remove the control. I can still find it after the removal ie I can reference tb2.


Question by:SamJolly
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 25

Assisted Solution

SStory earned 100 total points
ID: 34906613
Does this help?

This line:

Should be


shouldn't it?

Author Comment

ID: 34906682
Should be


shouldn't it?

Absolutely....... I changed the code for my question, but included a typo ......

I have already read that article... I think the problem is that you can only remove control in the crrent context ie you have to navigate to the relevant cell and uses its remove method

ie Cells(0).RemoveAt(0)

LVL 20

Expert Comment

ID: 34907666
I would really like 2 knw why u wud remove it. U dnt want 2 render it on page? If tht's the case u can set visible=false
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

LVL 20

Expert Comment

ID: 34907848
I would really like 2 knw why u wud remove it. U dnt want 2 render it on page? If tht's the case u can set visible=false
LVL 10

Accepted Solution

P1ST0LPETE earned 400 total points
ID: 34908977
You are correct Sam.  You have to remove controls from there current context:

Consider the following code which works, in fact if you step through the code you can see the count value change:

<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
        <asp:Panel ID="Panel1" runat="server">
            <asp:TextBox ID="textbox1" runat="server" Text="textbox 1" />

Open in new window

Code Behind:
public partial class DynamicControls : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
        if (!IsPostBack)
            int count = Panel1.Controls.Count;

            TextBox textbox2 = new TextBox();
            textbox2.ID = "textbox2";
            textbox2.Text = "textbox 2";

            count = Panel1.Controls.Count;

            TextBox textbox1 = (TextBox)Panel1.FindControl("textbox1");

            count = Panel1.Controls.Count;

Open in new window

However if I changed lines 16 and 17 of the above code behind to this:
TextBox textbox1 = (TextBox)this.FindControl("textbox1");

Open in new window

Then you would notice that the count value would not change at the end because textbox1 would not have been removed.  The FindControl() method would indeed find textbox1, but the Controls.Remove() method would not remove it because textbox1 is not a direct child of the page (i.e. "this").  This is the same thing you were running into as the FindControl() method iterates through all nested children controls and found your Textbox, but because the Textbox was not a direct child of your Table control, the Textbox was not removed.

Author Closing Comment

ID: 34925014

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit If you want to manage em…

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question