Solved

Trying to remove controls dynamically from ASP.NET page.

Posted on 2011-02-16
6
322 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

770 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