如何以编程方式在MasterPage上使用ToolkitScriptManager替换ScriptManager

时间:2014-05-12 13:45:45

标签: c# asp.net .net ajaxcontroltoolkit

我正在使用一个使用包含ScriptManager的MasterPages的旧版Web应用程序。我需要添加利用ToolScriptManager的内容页面,因此,我尝试以编程方式查找旧版本并使用新版本替换旧控件。当我逐步执行代码时,在删除ScriptManager之后,它仍然存在,即使在回发后也是如此。我尝试了许多不同的方法,但我概述了下面的一般想法。提前感谢任何建议。

MasterPage.master包含:

<asp:scriptmanager id="ScriptManagerOld" runat="server"></asp:scriptmanager>

MasterPage.master.cs包含:

public ScriptManager MasterScriptManager
{
    get{return ScriptManagerOld;}
}

ContentPage.aspx.cs包含:

protected void Page_Load(object sender, EventArgs e)
{
    ReplaceScriptManager();
}

private void ReplaceScriptManager()
{
    try
    {
        Page page = this.Page;

        //remove any instances of non-toolkit script manager
        // attempt 1
        if (Master.MasterScriptManager != null)
        {
            ScriptManager smngr = Master.MasterScriptManager;
            page.Controls.Remove(smngr);
            smngr.Dispose();
        }
        // attempt 2
        if (Master.MasterScriptManager != null)
        {
            ScriptManager smngr = Master.MasterScriptManager;
            page.Controls.Remove(smngr);
            page.Controls.Remove(smngr.Parent);
            smngr.Dispose();
        }
        // attempt 3
        if (ScriptManager.GetCurrent(page) != null)
        {
            ScriptManager smngr = ScriptManager.GetCurrent(page);
            page.Controls.Remove(smngr);
            smngr.Dispose();
        }
        // Add new ctrl
        ToolkitScriptManager scriptMngr = new ToolkitScriptManager();
        scriptMngr.ID = "ToolkitScriptManager";
        scriptMngr.EnablePageMethods = true;
        scriptMngr.CombineScripts = false;
        // Exception on next line... cannot add two instances of scriptmanager
        page.Page.Form.Controls.Add(scriptMngr);
    }
    catch (Exception ex)
    {
        throw (ex);
    }
}
protected void Button1_Click(object sender, EventArgs e)
{
    ReplaceScriptManager();
}

1 个答案:

答案 0 :(得分:0)

替换此标记:

<asp:scriptmanager id="ScriptManagerOld" runat="server"></asp:scriptmanager>

<asp:ToolkitScriptManager id="ScriptManagerOld" runat="server"></asp:ToolkitScriptManager >

在你的母版页中,足够的ToolKit管理器是向后兼容的。

如果你想修改MOSS中的控件,你需要在&#34; PreInit&#34;中进行修改。事件

这是您添加ScriptManager OnPreInit

的方法
protected override void OnPreInit(EventArgs e)
    {
        ScriptManager scriptManager = ScriptManager.GetCurrent(this);
        if (scriptManager == null)
        {
            scriptManager = new ScriptManager();
            this.Page.Form.Controls.AddAt(0, scriptManager);
        }


        base.OnPreInit(e);
    }