简体   繁体   English

如何通过C#从SharePoint页面删除ScriptManager

[英]How to remove ScriptManager from SharePoint Page via C#

I am trying to remove the Scriptmanager from a SharePoint page in code so I can swap it with the Ajax toolkit. 我正在尝试从代码中的SharePoint页面删除Scriptmanager,以便可以与Ajax工具包交换它。 However, when I use the following code I get: 但是,当我使用以下代码时,我得到:

Only one instance of a ScriptManager can be added to the page

The code errors on the Add statement. Add语句上的代码错误。 This has to be done via code as access to modify the master page source is not an option. 这必须通过代码完成,因为不能修改母版页源。

The code from my webpart that needs to replace the scriptmanager: 我的Webpart中需要替换脚本管理器的代码:

protected void GroupCalenderSourceControl_Init(object sender, EventArgs e)
    {
        //throw new NotImplementedException();
        ScriptManager sm = ScriptManager.GetCurrent(Page);

        if (sm == null)
        {
            Page.Controls.Add(new AjaxControlToolkit.ToolkitScriptManager());
        }
        else
        {
            if (!(sm is AjaxControlToolkit.ToolkitScriptManager))
            {
                Page.Controls.Remove(Page.FindControl(sm.ID));
                Page.Master.Controls.Remove(Page.FindControl(sm.ID));
                sm = null;
                Page.Controls.Add(new AjaxControlToolkit.ToolkitScriptManager());
            }
        }
    }

The main problem is that the page needs to complete the post-back to fully unregister the original script manager. 主要问题是页面需要完成回发才能完全注销原始脚本管理器。 Doing it in the same code block isn't going to work too well. 在同一代码块中执行此操作不会很好。

However, having read around this subject it seems AJAX Control Toolkit controls work perfectly fine with the standard ASP.NET ScriptManager. 但是,阅读此主题后,看来AJAX Control Toolkit控件可以与标准ASP.NET ScriptManager完美配合。 Can you not stick with that instead? 你不能坚持吗?

Whilst ToolkitScriptManager is meant to improve some of the ScriptManager's behaviors in particular how it renders out behavior JS scripts ScriptManager will execute its requests as fast as ToolkitScriptManager 虽然ToolkitScriptManager旨在改善ScriptManager的某些行为,尤其是它如何表现行为JS脚本ScriptManager将以与ToolkitScriptManager一样快的速度执行其请求

Source: http://blog.turlov.com/2008/05/scriptmanager-vs-toolkitscriptmanager.html 来源: http//blog.turlov.com/2008/05/scriptmanager-vs-toolkitscriptmanager.html

Try with this code 尝试使用此代码

  foreach (Control ctr in Page.Form.Controls)
        {
            if (ctr.GetType() == typeof(System.Web.UI.ScriptManager))
            {
                Page.Form.Controls.Remove(ctr);
                break;
            }
        }
     if (ScriptManager.GetCurrent(this.Page) == null)
         {
             ScriptManager scriptManager = new ScriptManager();
             scriptManager .ID = "scriptManager" + RandomNo;
             Page.Form.Controls.AddAt(0, scriptManager );
         }

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

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