Cannot unregister updatepanel since it was not registered with the scriptmanager error

Cannot unregister updatepanel since it was not registered with the scriptmanager error

This error is a pesky one. You will most likely encounter it when you’re trying to dynamically load an UpdatePanel within an itemTemplate in a datagrid, or some similar control with dynamically created items. In my case: in a datagrid, and specifically within an item template in that grid.

 

 

 

 

Here is the solution that has worked for me:

1) Hook into the Unload event of the updatepanel like so (in ASPX or ASCX file):

<asp:UpdatePanel runat="server" ID="updatePanel" OnUnload="updatePanel_Unload"> <ContentTemplate> </ContentTemplate> </asp:UpdatePanel>

2) Now in your code behind, use this implementation of Unload.
(*You will need a reference to System.Linq and System.Reflection if you don’t already have one)

protected void updatePanel_Unload(object sender, EventArgs e)
{
    /* Cast sender as an updatePanel, and use reflection to invoke * * the page's scriptmanger registerUpdatePanel() method * * */ 
UpdatePanel aUpdatePanel = sender as UpdatePanel;

    MethodInfo m =(
        from methods in typeof(ScriptManager).GetMethods(
            BindingFlags.NonPublic | BindingFlags.Instance
            )
        where methods.Name.Equals("System.Web.UI.IScriptManagerInternal.RegisterUpdatePanel")
        select methods).First<MethodInfo>();

    m.Invoke(ScriptManager.GetCurrent(aUpdatePanel.Page), new object[] { aUpdatePanel });
}

Executing the reflection code to invoke the scriptManager’s registerUpdatePanel() method should fix your problem and you’re off and running again!

By | 2018-03-07T02:55:22+00:00 October 28th, 2009|Categories: ASP.NET|Tags: , , , |7 Comments

About the Author:

7 Comments

  1. Andrei April 6, 2011 at 11:13 am - Reply

    Thank you very much. It fixed my issue. Amazing!!!

  2. JWolt June 2, 2011 at 11:25 am - Reply

    This is the only solution that has worked for me. Thank you!

  3. M. Hammad August 28, 2011 at 6:55 am - Reply

    Thank you, works like magic

  4. Ramya October 29, 2011 at 9:57 pm - Reply

    What is aUpdatePanel?
    Is that UpdatePanel ID?

    In the above example I see ID=”updatePanel”.

    Should it be updatepanel instead of aUpdatePanel?

    Can you please clear my doubt?

    Mine is still refreshing.

    • Brent October 31, 2011 at 8:48 pm - Reply

      Hello Ramya,

      aUpdatePanel is actually from this line: UpdatePanel aUpdatePanel = sender as UpdatePanel;. The sender object is being cast to an updatePanel and then used.

      When you say it is “still refreshing”, are you seeing any type of error? I’m wondering if this post is not what you’re looking for? This is for correcting issues around several updatepanels being dynamically added to a page.

  5. Jonatas Cezarino January 8, 2014 at 4:47 am - Reply

    thank you so much my friend! I was almost going crazy!! 😉

  6. samuel February 28, 2014 at 11:20 am - Reply

    This fix works well just that sometimes it gives an index out of range error. anyone encountered

Leave A Comment