Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Trying to remove controls dynamically from ASP.NET page.

Posted on 2011-02-16
6
Medium Priority
?
330 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
[X]
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
6 Comments
 
LVL 25

Assisted Solution

by:SStory
SStory earned 400 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 1600 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

722 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