views:

23

answers:

2

The CMS I use struggles with UpdatePanels in some of my custom controls and master pages during edits. I'd like to convert them dynamically to regular panels on Page_Load or Page_Init or something based on whether the page is being edited.

I'd like to convert all the UpdatePanels on a page to Panels dynamically. Finding the UpdatePanels isn't an issue, all my pages inherit from a common base class, and all my controls inherit from a common base class -- so I can override the Page_Init or whatever.

I suspect I can't covert the UpdatePanel to a regular Panel. I thought about maybe finding the UpdatePanel, adding a Panel to the UpdatePanel's parent, then looping through each of the UpdatePanel's controls and adding them to the new Panel, then removing the UpdatePanel.

But if I add a new Panel, it'll be at the end, can you add a Panel in the middle... maybe with Insert? This shouldn't be difficult, but am I making it too difficult? Is there a simpler way? Anybody ever done stuff like this?

Thanks, Eric

Update I ended up override the OnInit function on my MasterPage base class to readd the UpdatePanel to the ScriptManager after it moved per Philippe's comment on http://msmvps.com/blogs/luisabreu/archive/2006/11/16/adding-removing-updatepanels-dynamicaly-from-a-page.aspx

protected override void OnInit(EventArgs e)
{
   if (Page is CMSPage && Page.IsDesignMode())
   {
      foreach (UpdatePanel up in this.FindControls<UpdatePanel>())
      {
         up.Load += UpdatePanel_Load;
      }
   }

   base.OnInit(e);
}

private void UpdatePanel_Load(object sender, EventArgs e)
{
   UpdatePanel panel = 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();

   if (panel == null || m == null)
      return;

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

My 2 cents...

Try to figure out the root cause of the issue. UpdatePanels shouldn't be causing you issues in the first place. Can you clarify what kind of errors you are getting WITH update panels?

Rahul Soni
Cannot unregister UpdatePanel with ID 'up' 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
Eric
How are you adding this 'up' updatepanel. Can u reproduce this error at will or does it happen at random. If u can repro, you should add trace statements and try debugging ur app.
Rahul Soni
I'm using a (closed source) 3rd party content management system, which is moving controls around during load (or init).
Eric
A: 

I'm pretty sure there is not going to be a way to convert an UpdatePanel to a Panel - at least no simple way. When you add an UpdatePanel to your page .Net will add a script reference with alot of javascript to make the UpdatePanel work. It's possible/likely that this is what your CMS is having issues with. Often these packages can run into name collisions with some of the .Net Ajax javascript functions like $get or something like that.

The only solution, if you insist on the model of sometimes using an UpdatePanel and sometimes not - would be to put a panel on the page and dynamically build the UpdatePanel in code behind everytime you want to use it.

I tend to agree with Raul that you should probably try to figure out another why. Try using some other ajax methods other then UpdatePanels (PageMethods, jQuery .ajax calls or good old fashioned javascript XMLHttprequest) if your CMS does not like them.

brendan
I hope to eliminate UpdatePanels in the future, at least on the heavily trafficked pages. But they're just so handy...
Eric