Adding A RadAjaxManager In A WebPart

This article on the Telerik site is actually not correct:

http://www.telerik.com/help/aspnet-ajax/ajax-ajxmanager-is-null-added-in-sharepoint-webpart.html

The problem with this is you will be modifying the control collection during an event when it can’t be modified. This will result in the error:

The control collection cannot be modified during DataBind, Init, Load, PreRender or Unload phases.

If you attempt to move the addition to a different event, generally when using something like a

[csharp]

 RadAjaxManager manager = RadAjaxManager.GetCurrent(Page);

[/csharp]

you will get an object reference error. The best way to do it is within the Init of the WebPart, and then capture the PreRender of the Page.

[csharp]

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            Page.PreRender += Page_PreRender;
         }

        void Page_PreRender(object sender, EventArgs e)
        {
            RadAjaxManager ajaxManager = RadAjaxManager.GetCurrent(Page);
            if (ajaxManager == null)
            {
                RadAjaxManager manager = new RadAjaxManager();
                manager.ID = “RadAjaxManager1”;
                Page.Items.Add(typeof(RadAjaxManager), manager);
                Page.Form.Controls.Add(manager);
            }
        }

[/csharp]

Share

Leave a Reply

Your email address will not be published. Required fields are marked *