简体   繁体   English

在后面的代码中将UpdatePanel转换为Panel

[英]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. 在编辑过程中,我在某些自定义控件和母版页中使用的CMS与UpdatePanels发生冲突。 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. 我想将它们动态地转换为Page_Load或Page_Init上的常规面板,或者基于是否正在编辑页面而将其转换为常规面板。

I'd like to convert all the UpdatePanels on a page to Panels dynamically. 我想将页面上的所有UpdatePanels动态转换为Panels。 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. 找到UpdatePanels没问题,我的所有页面都继承自一个公共基类,我的所有控件都继承自一个公共基类-因此我可以覆盖Page_Init或其他任何东西。

I suspect I can't covert the UpdatePanel to a regular Panel. 我怀疑我无法将UpdatePanel隐藏到常规面板中。 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. 我考虑过可能要找到UpdatePanel,将面板添加到UpdatePanel的父级,然后遍历每个UpdatePanel的控件,然后将它们添加到新的Panel中,然后删除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 更新最后,我根据Philippe在http://msmvps.com/blogs/luisabreu/archive/2006/11/16/adding-removing上的注释将其移动后,在MasterPage基类上覆盖了OnInit函数以将UpdatePanel读取到ScriptManager -updatepanels-dynamicaly-from-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. 我很确定不会有将UpdatePanel转换为Panel的方法-至少没有简单的方法。 When you add an UpdatePanel to your page .Net will add a script reference with alot of javascript to make the UpdatePanel work. 当您将UpdatePanel添加到页面时,.Net将添加带有大量JavaScript的脚本引用,以使UpdatePanel正常工作。 It's possible/likely that this is what your CMS is having issues with. 这可能/可能是您的CMS遇到的问题。 Often these packages can run into name collisions with some of the .Net Ajax javascript functions like $get or something like that. 通常,这些包可能会与某些.Net Ajax javascript函数(例如$ get或类似的东西)发生名称冲突。

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. 如果坚持使用有时使用UpdatePanel而有时不使用UpdatePanel的模型,则唯一的解决方案是在页面上放置一个面板,并在每次使用它时在代码后动态构建UpdatePanel。

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. 如果您的CMS不喜欢,请尝试使用UpdatePanels之外的其他ajax方法(PageMethods,jQuery .ajax调用或老式的JavaScript XMLHttprequest)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM