Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old June 10th, 2004, 12:18 PM
Authorized User
Join Date: Sep 2003
Posts: 93
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to flyin
Default Nested User Controls Error

A control cannot modify its parents' control collections.

That is the error.


I made a Paging User Control that is actually pretty cool. Within the User Control it uses Page.RegisterStartupScript() to register some javascript with the current .aspx page that forces a page postback.

I use the Statement to dynamically register a Server Control so that the Paging User Control will find the current page clicked Correctly

CType(Page.FindControl("Form1"), HtmlForm).Controls.Add(hdnCurrentPage)

So With registering this Server Control, along with my javascript, i can figure out what page number the client has clicked on by simply doing the

Dim CurrPage as Integer = Integer.Parse(
Ctype(Page.FindControl("hdnCurrentPage"), HtmlHiddenInput).Value

Well, For Some reason, when this Paging user Control is used within a .aspx page, it has no problem with the statement:

CType(Page.FindControl("Form1"), HtmlForm).Controls.Add(hdnCurrentPage)

But, when i put the paging user control within another user control, i get the error:

"A control cannot modify its parents' control collections."

Any thoughts? IF you would like me to elaborate with more code i would be more than happy to provide! Thanks all!

Old June 11th, 2004, 11:34 AM
planoie's Avatar
Friend of Wrox
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts

If I may, I'd like to suggest a design change:

The way you are doing this indicates that it could be a future problem (apart from the difficultly you are already having). If you creating a paging control, and that control needs to add a control to maintain some information (i.e. in your case, this hidden control), that control should live within the context of the usercontrol that created it. Why try to put it outside in the containing page? What happens when you have two instances of this control on the same page? It would make sense to me to just add that hidden control into the user control's Controls collection. Then it's withing the context of the user control, and not the page. Any addition instances of the user control will have their own hidden control named appropriately to indicate their context. Furthermore, because the child hidden control is in the user control's Controls collection, you shouldn't need to deal with finding it to get its value back.

I suspect the reason you are doing this is due to the javascript you are using. There are ways to work with javascript that allow you to instantiate multiple instances of the same control that uses the javascript.

One other thing I noticed is that you have "Form1" hard coded in. This will break the instant someone uses this control and changes the form name. Odd that someone would do this, but it happens.
Old June 11th, 2004, 11:45 AM
Authorized User
Join Date: Sep 2003
Posts: 93
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to flyin

Thank you for the input. For some reason i want to say that tried to instantiate the javascript and the paging number value within the context of the user control, but it couldn't find the hidden server control for some reason. I will try it again and register the control with the Mybase.Controls.Add of the UserControl instead of the Page.Controls.Add. Maybe i was just overlooking something. THanks for the input. I'll give some feedback as to my progress thanks.


Old June 11th, 2004, 12:13 PM
Authorized User
Join Date: Sep 2003
Posts: 93
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to flyin

Let me add another sub question to this problem.


Within this Paging User Control I have it output dynamic Javascript according to the number of pages that belong to the current set of data. So for instance i have something like the following:
        For i = _start To _end
            If _PageNumber <> i Then
                Me.PagesDisplay.Text += "<a href=""javascript:ChangePage(" & i & ")"">" & i & "</a>&nbsp;&nbsp;"
                Me.PagesDisplay.Text += "[" & i & "]&nbsp;&nbsp;"
            End If
        Next i
Note: PagesDisplay Is a Label Control. and _PageNumber queries the Hidden Input Field hdnCurrentPage to figure out what page we are currently on.

which outputs in html the following:

<span id="test1_ucPagebarTop_PagesDisplay">[1]&nbsp;&nbsp;
<a href="javascript:ChangePage(2)">2</a>&nbsp;&nbsp;<a href="javascript:ChangePage(3)">3</a>&nbsp;&nbsp;</span>

Also within the usercontrol i specify the javascript function:

<script language="javascript">
function ChangePage(id)
  document.all.hdnCurrentPage.value = id;

And in the code behind of the Pager.ascx i have a method that
handles the postback for the btnPageNumbersHolder Server Control which is essentially a visible=false button server control so that when a person clicks on a page number i can have it postback.

Well, This is obviously the problem because i have to have javascript find a hidden input server control and set it so that in the code behind of the pager user control it can then find the hidden field (hdnCurrentPage) to figure out what page the user has clicked on and then display the correct information.

Well now to my question, Is there a way to dynamically register a set of server controls such that when clicked, it will post back with the correct page number that was clicked? this would solve my problems. Or do you see something different all together that i am overlooking?

The problem is essentially that i will have a dynamic set of page numbers and i need to have the page post back with the proper page number that was clicked and figure it out appropriately then i can go from there.

Am i making sense? shall i elaborate more?

Similar Threads
Thread Thread Starter Forum Replies Last Post
User controls dcct84 C# 2 October 28th, 2007 07:20 PM
Accessing Child Controls of Nested Repeater... Jayahar ASP.NET 2.0 Professional 3 October 11th, 2007 01:24 PM
User controls' content: Chapter 2 User Controls AGS BOOK: Professional ASP.NET 2.0 Server Control and Component Development ISBN: 978-0-471-79350-2 10 July 26th, 2007 05:36 AM
User Controls Duncan Pro VB.NET 2002/2003 1 October 27th, 2003 12:51 PM
User Controls raZZa VS.NET 2002/2003 3 July 30th, 2003 07:36 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.