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:
- 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;
- 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;
- 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:
- during the Init event, the UpdatePanel registers itself with the ScriptManager control;
- when it's removed from a control's collection, the UpdatePanel unregisters itself with the ScriptManager control;
- the UpdatePanel is added again to another control's collection
- during the OnUnload event of the page, the UpdatePanel tries to unregister with ScriptManager
- 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.