Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
323 views
in Technique[技术] by (71.8m points)

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...