Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

"A control cannot modify its parents' control collections"

Status
Not open for further replies.

BrianB

Programmer
Oct 2, 2000
186
US
I have a standard web form that includes a custom web control, EditHost. EditHost is used as a container for all online editing functions like modifying people records, etc. Depending on user permissions, some controls such as AdministerUsers get loaded, some don't.

We have been dynamically loading the EditPerson custom web control, which itself contains other custom web controls representing multiple phone numbers, etc. All good OO design.

Trouble is, that when one clicks a link in the EditPhoneNumber control, the whole page reposts and recreates the parent form's controls, throwing

"A control cannot modify its parents' control collections"

We aren't trying to modify the parent forms' controls. Our great-grandchild form is just reacting to a postback, which forces the controls to be recreated.

How do we avoid this?

-Brian
 
I have had this happed to me to. I got around it by adding controls on the parent page that are in a placeholder.

PARENT PAGE:
Code:
<form id="Form1" method="post" runat="server">
			<uc1:Thechild id="Thechild1" runat="server"></uc1:Thechild>
			<asp:Label id="Label1" runat="server">Label</asp:Label>
			<asp:PlaceHolder ID=plcHolder1 Runat=server>
			
			</asp:PlaceHolder>
		</form>


THE CHILD
Code:
<%@ Control Language="vb" AutoEventWireup="false" Codebehind="Thechild.ascx.vb" Inherits="Examples.Thechild" TargetSchema="[URL unfurl="true"]http://schemas.microsoft.com/intellisense/ie5"[/URL] %>
<%@ Register TagPrefix="uc1" TagName="GrandChild" Src="GrandChild.ascx" %>
<uc1:GrandChild id="GrandChild1" runat="server"></uc1:GrandChild>


THE GRAND CHILD:
Code:
Dim lbl As Label = Me.Parent.Parent.FindControl("Label1")
        lbl.Text = "Yes I can write to label on parent"

        Dim label2 As New Label
        label2.Text = "Adding another label"
'Me.Parent.Parent.Controls.Add(label2) This throws the error


        Dim plc As PlaceHolder = Me.Parent.Parent.FindControl("plcHolder1")
        plc.Controls.Add(label2) 'This does not throw an error
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top