"Cannot unregister UpdatePanel with ID 'xxx' since it was not registered with the ScriptManager... " in RadGrid while editing record

wegelagerer picture wegelagerer · Jan 4, 2012 · Viewed 19.8k times · Source

Let me cut to the chase. My scenario is as follows: I have custom added fields to filter the RadGrid and filtering works perfectly. The problem comes when I want to edit record using EditForm inside RadGrid. It used to work fine, but then I had some problems with selecting the right row (I was always getting the wrong row selected) so this is what I did to fix it.

So, my RadGrid with filters looks like this:

enter image description here

What I did is to use the Session which will help us to determine later if the filtered RadGrid DataSource was initiated or it was the default one.

protected void btnSearch_Click(object sender, EventArgs e)
{
    Session["SearchKontakti"] = "1";
}

After that I had to set PreRender with if loop to check for previously mentioned Session.

protected void gvKontakti_PreRender(object sender, EventArgs e)
{
    int idKontakt = Convert.ToInt32(Request.QueryString["idk"]);

    if (Session["SearchKontakti"] == "1")
    {
        var kontakti = from k in db.Kontakt
                       select k;

        int idTipUsera = Convert.ToInt32(rcbTipUsera.SelectedValue);
        int idTvrtka = Convert.ToInt32(rcbTvrtka.SelectedValue);

        if (rcbTvrtka.SelectedValue != "0")
        {
            kontakti = kontakti.Where(k => k.idFirma == idTvrtka);
        }

        if (rcbTipUsera.SelectedValue != "0")
        {
            kontakti = kontakti.Where(k => k.idOvlasti == idTipUsera);
        }

        if (chkAktivan.Checked == true)
        {
            kontakti = kontakti.Where(k => k.Aktivan == true);
        }
        else
        {
            kontakti = kontakti.Where(k => k.Aktivan == false);
        }

        int idAuthKontakt = Convert.ToInt32(Session["authenticatedUI"]);

        if (idKontakt > 0 && idAuthKontakt == idKontakt)
        {
            gvKontakti.DataSource = from k in kontakti
                                    where k.idKontakt == idKontakt
                                    orderby k.Prezime, k.Ime
                                    select new { Tvrtka = k.Firma.Naziv, k.idKontakt, Naziv = k.Ime + " " + k.Prezime, Funkcija = k.Funkcija, k.Ime, k.Prezime, k.Tel1, k.Tel2, k.Mob1, k.Mob2, k.Email1, k.Email2, k.Fax, k.Adresa1, k.Adresa2, k.Adresa3, k.Grad, k.PostanskiBroj, k.Drzava, k.Biljeske, k.Aktivan, k.Username, k.Password };
        }
        else if (idKontakt > 0 && idAuthKontakt != idKontakt)
        {
            gvKontakti.DataSource = from k in kontakti
                                    where k.idKontakt == idKontakt
                                    orderby k.Prezime, k.Ime
                                    select new { Tvrtka = k.Firma.Naziv, k.idKontakt, Naziv = k.Ime + " " + k.Prezime, Funkcija = k.Funkcija, k.Ime, k.Prezime, k.Tel1, k.Tel2, k.Mob1, k.Mob2, k.Email1, k.Email2, k.Fax, k.Adresa1, k.Adresa2, k.Adresa3, k.Grad, k.PostanskiBroj, k.Drzava, k.Biljeske, k.Aktivan, k.Username, k.Password };
        }
        else
        {
            gvKontakti.DataSource = from k in kontakti
                                    orderby k.Prezime, k.Ime
                                    select new { Tvrtka = k.Firma.Naziv, k.idKontakt, Naziv = k.Ime + " " + k.Prezime, Funkcija = k.Funkcija, k.Ime, k.Prezime, k.Tel1, k.Tel2, k.Mob1, k.Mob2, k.Email1, k.Email2, k.Fax, k.Adresa1, k.Adresa2, k.Adresa3, k.Grad, k.PostanskiBroj, k.Drzava, k.Biljeske, k.Aktivan, k.Username, k.Password };
        }

        gvKontakti.DataBind();
    }
}

So, this fixed my primary problem, but gave me another one. Some of my UserControls contain UpdatePanel and for each UserControl that has it whenever I try to clik Edit button from the RadGrid I receive the following message: "Cannot unregister UpdatePanel with ID 'UpdatePanel4' since it was not registered with the ScriptManager. This might occur if the UpdatePanel was removed from the control tree and later added again, which is not supported. Parameter name: updatePanel"

What I'd like to know is how to fix it.

Regards,

Hrvoje

Answer

Ali Dehghan Tarzeh picture Ali Dehghan Tarzeh · Feb 5, 2012

I don't know why, but somehow the UpdatePanel is unregistered from the ScriptManger twice (it happens in RadGrid.Rebind() method too; the situation I was stuck in), and the second time it's unregistered from ScriptManger you get the "Cannot unregister UpdatePanel ..." error.

The workaround is to register the UpdatePanel with the ScriptManger somewhere between the two unregister events, using reflection, like this:

protected void UpdatePanel_Unload(object sender, EventArgs e) {
    MethodInfo methodInfo = typeof(ScriptManager).GetMethods(BindingFlags.NonPublic | BindingFlags.Instance)
        .Where(i => i.Name.Equals("System.Web.UI.IScriptManagerInternal.RegisterUpdatePanel")).First();
    methodInfo.Invoke(ScriptManager.GetCurrent(Page),
        new object[] { sender as UpdatePanel });
}

you should add the UpdatePanel_Unload to the OnUnload event of the UpdatePanel:

<asp:UpdatePanel ID="UpdatePanel1" runat="server" OnUnload="UpdatePanel_Unload">

You can find the complete details of the problem here