存在ScriptManagerProxy控件时以编程方式禁用ScriptManager

时间:2010-06-04 16:24:35

标签: asp.net asp.net-ajax scriptmanager

我尝试过以下建议,以编程方式禁用ScriptManager,但无济于事: Disable ScriptManager on certain pages

StandardScriptManager.ascx

<%@ control language="vb" autoeventwireup="false" codebehind="StandardScriptManager.ascx.vb" inherits="StandardScriptManager" %>
<h1>StandardScriptManager is visible</h1>
<asp:scriptmanager id="MyScriptManager" runat="server" enablepartialrendering="true" >
        <scripts>
            <asp:scriptreference path="/Standard/Core/Javascript/script1.js" />
            <!-- etc... -->
        </scripts>
</asp:scriptmanager>

StandardScriptManager.ascx.vb

Partial Public Class StandardScriptManager
    Inherits System.Web.UI.UserControl

    Private _ScriptManager As ScriptManager

    Private Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        If DisableAllScripts Then
            Me.Visible = False
        End If
    End Sub

End Class

如果DisableAllScripts为真,则<h1>不会显示,但脚本仍会添加到页面中。我怀疑这是因为我在页面的其他地方有ScriptManagerProxy对象。

我还在Me.Controls.Clear()中尝试了Page.Init,但我得到了这个

[InvalidOperationException: Page cannot be null. Please ensure that this operation is being performed in the context of an ASP.NET request.]
   System.Web.UI.ScriptManager.get_IPage() +372796
   System.Web.UI.ScriptManager.OnPageInitComplete(Object sender, EventArgs e) +13
   System.Web.UI.Page.OnInitComplete(EventArgs e) +8699478
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +467

令人抓狂的是,没有一种简单的方法来禁用ScriptManager;该控件没有Enabled属性,您无法设置ScriptManager.Visible=False

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

我无法让这个工作,但我今天找到了解决方案。

如果您使用的是.Net 4.0,则可以使用新的AjaxFrameworkMode属性并将其设置为Disabled。

ScriptManager.AjaxFrameworkMode Property

  

使用AjaxFrameworkMode属性启用所有Microsoft Ajax脚本   文件,禁用所有Microsoft Ajax脚本文件,或显式   包括单个脚本文件。

ScriptManager1.AjaxFrameworkMode = AjaxFrameworkMode.Disabled

希望帮助那些像我一样在这里结束的人......