Convert UpdatePanel to Panel in code behind
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 a开发者_JAVA技巧n 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 });
}
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.
精彩评论