Adding/removing UpdatePanels dynamicaly from a page

Today I've found another post on the AJAX discussion forums asking info about the "Cannot unregister UpdatePanel with ID XXX since it was not registered with the ScriptManager.This might occur if the UpdatePanel was removed from the control tree and latter added again".

This happens only in dynamic pages, when you have UpdatePanels added to a certain control and then you move it to another control's collection after the init event(ie, you remove the control from the 1st control's collection and add it to the 2nd one). The solution to this is to perform these operations until the end of the PreInit event. If you're pratical and really don't care why this happens, then it's safe to scroll the page or go to the next post (if you're one of the 3 guys that have subscribed this blog ;))

ok, still here? then let's proceed...to understand why you get that error, you must know the following:

  1. during the Init event, the UpdatePanel control registers itself with the ScriptManager control. You can think of this as putting the UpdatePanel in a list maintained by the ScriptManager control;
  2. during the Unload event, the UpdatePanel unregisters itself with the ScriptManager control. As you've already guessed, this means that the UpdatePanel is removed from the internal list mentioned in 1;
  3. you get the previous error when you try to remove an UpdatePanel which is no longer on the internal list maintained by the ScriptManager control

hum, ok...but why am I getting this error? well, what many still don't know is that when you remove a control from a control's collection, the unload method of the removed control called. Yes, that's right! don't believe me? Ok, use reflector and take a look at the RemoveAt method of the ControlCollection class...so, when you remove the control from a control's collection and add it to another, that UpdatePanel will be removed from the internal list maintained by the ScriptManager before time. following? let's enumerate the steps:

  1. during the Init event, the UpdatePanel registers itself with the ScriptManager control;
  2. when it's removed from a control's collection, the UpdatePanel unregisters itself with the ScriptManager control;
  3. the UpdatePanel is added again to another control's collection
  4. during the OnUnload event of the page, the UpdatePanel tries to unregister with ScriptManager
  5. since it's not in the list, ScriptManager generates an exception

hum...but when I added it to the 2nd control's collection (as in the previous example), shouldn't the Init method be called again, resulting in  a new registration? Well, it depends...you see, internally, each control "knows" its current state. So, if you're removing/adding the UpdatePanel during the Load event, when you add it, you won't get the Init call because you're adding the some control and it has already handled the Init event. On the other hand, if you move the UpdatePanel during the PreInit event you won't get into trouble.

Do keep in mind that this is only valid for removing/adding UpdatePanels maitained in a page. If you're just creating a new UpdatePanel, then you shouldn't get this kind of errors.

Filed under: ,

Comments

# Chris said:

Thanks for the post.  I follow why this is happening, but I added the controls to the page from the control panel in VS 2005.  Would you mind posting a possible solution. I do not know how to carry the Init event.  I'm fairly new to coding so, please elaborate.

Thursday, November 16, 2006 6:02 PM
# David said:

Thanks for the good explanation. Unfortunately, i am droping a user control into a custom Panel control that frames the user control in some common HTML plumbing - a bit like a webpart does for a user control. Anyhow, the user control has an embedded update panel. The custom Panel control overrides the CreateChildControls() method and takes any nested controls (the user control in this instance) and inserts them into the correct HTML container control. This is obviously where the UpdatePanel unregisters itself. How would I force the control tree construction to occur after the PreInit phase? I cant see how i can effect he timing of the control construction.

Thursday, December 14, 2006 7:11 PM
# Rob said:

I'm having the same problem as David.

Friday, December 15, 2006 3:13 PM
# Rob said:

I found a kludge that works for some very specific cases.  Here it is:

I'm no longer inheriting from Panel.  I inherit from WebControl.

I added an ITemplate property to my my Control.

I created ANOTHER control that implements INamingContainer.  Within the constructor of this container I create all my framing controls except for user controls (keep reading).  For those, I have private properties for their containers once they're instantiated.

My CreateChildControls method instantiates a new sub-container control.  Then I call MyTemplate.InstantiateIn(mySubContainer.MyExposedContentContainer) THEN I add this sub-container to this.Controls.

The problem is that I have to instantiate the user controls in preRender and disable their viewstate otherwise my viewstate gets out of whack.  It works for me since those controls are stateless.  Is there another way around this?  Am I doing something out of order?

I would like to be able to instantiate those user controls much earlier than prerender, since I know down the line I, or someone else will need to have programmatic access to them.

Friday, December 15, 2006 6:06 PM
# Morten said:

So basically this means that I can't add an updatepanel in createchildcontrols, since this occurs after OnInit? Hmm...

Thursday, March 29, 2007 3:02 PM
# cmwalolo said:

dev.cmwa.eu/.../Default.aspx

Here some sample that can make it ... The idea is to load your customcontrols only on the prerender.

I made some test pages with some PanelManagement class. That can handle this easily.

Friday, May 04, 2007 5:41 AM
# Loco said:

cmwalolo.... can't seem to find the link to download your sample. I'm getting the dreaded "Cannot unregister UpdatePanel " error too. Help a brother out.

Tuesday, May 08, 2007 1:57 AM
# Luis Abreu said:

Hello.

I believe that doing that might work since you're loading your control. It wont work, however, if you load you control (that has your panel) and then remove it from the current hierarchy and add it as a child control of another control.

Tuesday, May 08, 2007 3:34 AM
# Constantine said:

Interesting...

Wednesday, May 23, 2007 9:21 AM
# Bassam said:

Hi there

well i am getting the same error which you have discussed about, and i am not able to understand what / how to get a fix round of it.

my scenario is that i have a user control, UC1. i will drop it in an UpdatePanel UP1 on a wizard control WC1 step page, on the Main .aspx page. like

WC1 contains UP1; UP1 contains UC1

i have the script manager declared rite under the form tag on the master page, and all seems good / smooth to me, but still I WILL GET THIS ERROR :(.

please help me over this error. Waiting for your reply.

- Bassam

Friday, June 08, 2007 10:03 AM
# Yannas said:

Interesting...

Sunday, June 10, 2007 7:47 AM
# Alexios said:

interesting

Thursday, June 14, 2007 5:28 AM
# Evripides said:

interesting

Friday, June 15, 2007 7:01 PM
# Halu said:

interesting

Sunday, June 17, 2007 7:24 PM
# Kymon said:

Interesting...

Tuesday, June 19, 2007 6:23 AM
# Odysseas said:

interesting

Wednesday, June 20, 2007 1:50 AM
# Mamadshah said:

interesting

Saturday, July 28, 2007 6:05 AM
# Iannis said:

Interesting...

Sunday, July 29, 2007 12:31 AM
# Interesting said:

If you mo-fo's don't know the answer, just say so..!

Some of us would actually appreciate the help.

Thursday, September 13, 2007 7:01 AM
# bakuuu said:

Great, but I have an another problem.

I have been trying to set programatically visible and unvisible of some Controls in my WebPage. On this Controls there are UpdatePanels.

On each control - one UpdatePanel.

I have to set visible = true/false in "Page_PreInit" method, it works on single test site, but...

In my project I have a MasterPage, and in method Page_PreInit a reference to my control is null, so I can't change visible, and masterpage has no PreInit method.

How to solve it ?  Maybe you know some other solution to dynamic hide controls with updatePanel in it.

Monday, September 24, 2007 7:20 AM
# Goldfinger said:

FOR GOD.... WRITE THE DAMN SOLUTION

Thursday, March 27, 2008 3:32 PM
# Serializer said:

Yet another example of how broken ASP.NET is.

Why design this great component model, yet not actually build in the flexibility to use it properly?

It makes building web applications ultimately take about 100x as long, rather than saving time as it's supposed to - since I seem to spend all my time figuring out how to work around ridiculous inflexibilities like this.

I can't use the Page_PreInit event to insert my user control containing the update panel - because Page_PreInit has fired *way* before my other nested controls are ready to set themselves up.

Microsoft recommend that dynamically-added controls should be added in the Load method. This dynamically added control contains a templated control, which in turn instances a further user control within itself - sometimes this control will also contain an UpdatePanel. I simply can't do this at PreInit - the other page controls that I need to insert into aren't available at this time.

Don't know what to do but somehow I will kludge this mess, then wait for MVC toolkit to drop so I can start building things *properly*.

Sunday, April 06, 2008 6:49 PM
# Luis Abreu said:

Hello.

yeah, the web forms model has some gotchas....

but do notice that you can still add the UpdatePanel during the load event. what you cannot do is add/remove/add again after the event i've mentioned...the problem I'm describing happens because there are some cases where a control (which internally contains an UpdatePanel) is initially added to an existing control, then removed and then added again to another controls's collection. if you do this, then be sure to do it until the preinit event.

If you're just adding a control to the page (and you're nov removing it/adding it to another place later) you should be able to do it until the end of the load event...

Monday, April 07, 2008 3:19 AM
# ASP.NET AJAX Forum Posts said:

OK. I've been working on this for 2 days, and scouring the forums for answers. I hve found many and

Thursday, April 10, 2008 4:52 PM
# Kominsky said:

Well, as som have said, there's a lot of complaining about this issue, but I can't see solutions, plx! if someone has a solution, post it here, I really need it, and well, I'm still lookin' for it, so if I get it, I'll post it here for sure.

My problem is:

aspx-contains the follow:

scriptManager1

   updatePanel1

        ascx1{updatePanel2[ascx2]}

when I try to update updatePanel2, I got that error...

By the way, my ascx 1 and ascx2 are loaded in the Page_Load cuz they are dynamic too...

PLX! HELP!

Wednesday, April 16, 2008 1:13 PM
# Kalam said:

I have migrated to .Net 2.0. There are some custom controls on the page including form and panel. This controls are mandatory for the page and form. Now while I want to implement ad ajax withih the page I get this error. How to deal with ajax and custom control in VS 2005. Any suggestion will be appriciated.

Please help

Thanks

Saturday, May 03, 2008 1:02 PM

Leave a Comment

(required) 
(required) 
(optional)
(required)