Solved

Trying to remove controls dynamically from ASP.NET page.

Posted on 2011-02-16
6
325 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

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…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

828 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