Solved

Trying to remove controls dynamically from ASP.NET page.

Posted on 2011-02-16
6
319 Views
Last Modified: 2012-05-11
Hi,

 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)
        tblTable.Controls.Remove(tb)
        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.

Thoughts?

Sam
0
Comment
Question by:SamJolly
6 Comments
 
LVL 25

Assisted Solution

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

http://www.codeproject.com/KB/user-controls/DynamicUC.aspx

This line:
  tblTable.Controls.Remove(tb)

Should be

  tblTable.Controls.Remove(tb1)

shouldn't it?
0
 

Author Comment

by:SamJolly
ID: 34906682
Should be

  tblTable.Controls.Remove(tb1)

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)

Sam
0
 
LVL 20

Expert Comment

by:informaniac
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
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 20

Expert Comment

by:informaniac
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
0
 
LVL 10

Accepted Solution

by:
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:

Markup:
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Panel ID="Panel1" runat="server">
            <asp:TextBox ID="textbox1" runat="server" Text="textbox 1" />
        </asp:Panel>
    </div>
    </form>
</body>
</html>

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";
            Panel1.Controls.Add(textbox2);

            count = Panel1.Controls.Count;

            TextBox textbox1 = (TextBox)Panel1.FindControl("textbox1");
            Panel1.Controls.Remove(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");
this.Controls.Remove(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.
0
 

Author Closing Comment

by:SamJolly
ID: 34925014
thks
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now