在某些页面上禁用 ScriptManager

发布于 2024-07-26 05:50:38 字数 171 浏览 8 评论 0原文

我的母版页上有一个脚本管理器。 我需要从一两个内容页面中删除 webresourse.axd,因为它会导致页面上的其他 javascript 出现问题

如何禁用这些页面上的脚本管理器?

ScriptManager 对象似乎没有任何看起来可以完成这项工作的属性,

这可能吗?

I have a script manager on my Master page. There are one or 2 content pages that I need to remove the webresourse.axd from, as it is causing issues with other javascript on the page

How can I disable the script manager on these pages?

The ScriptManager object doesnt appear to have any properties that would seem like they would do the job

Is this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

烙印 2024-08-02 05:50:38

将您的 移动到自定义控件,例如 MyScriptManager.ascx - .ascx 文件中的唯一代码将是 ScriptManager 标记 - 然后您可以设置 Visible< /strong> 自定义控件上的属性来控制是否呈现 ScriptManager。

<foo:MyScriptManager id="scriptManager" runat="server" Visible="false" />

您甚至可以向 MasterPage 添加一个属性,您可以在内容页面中使用该属性来显示/隐藏 ScriptManager:

// In your master page
public bool ShowScriptManager {get; set;}

// In your master page's Page_Load
private void Page_Load(object sender, EventArgs e) {
    ...
    scriptManager.Visible = ShowScriptManager;
    ...
}

由于大多数页面都需要 ScriptManager,因此将其默认为 true 可能是一个主意 - 我认为您可以在母版页的 Page_Init 方法的构造函数中执行此操作:

public SiteMaster() {
    ...
    ShowScriptManager = true;
    ...
}

// Or alternatively
private void Page_Init(object sender, EventArgs e) {
    ...
    ShowScriptManager = true;
    ...
}

然后,如果您在内容页中设置了 MasterType:

<%@ MasterType VirtualPath="~/path/to/master/page" %>

您只需在内容页的 Page_Load 中执行类似的操作:

Master.ShowScriptManager = false;

Move your <asp:ScriptManager /> to a custom control e.g. MyScriptManager.ascx - the only code in the .ascx file will be the ScriptManager tag - then you can set the Visible property on your custom control to control whether the ScriptManager gets rendered.

<foo:MyScriptManager id="scriptManager" runat="server" Visible="false" />

You could maybe even add a Property to your MasterPage which you could use in your content pages to show / hide the ScriptManager:

// In your master page
public bool ShowScriptManager {get; set;}

// In your master page's Page_Load
private void Page_Load(object sender, EventArgs e) {
    ...
    scriptManager.Visible = ShowScriptManager;
    ...
}

As most of your pages require the ScriptManager, it might be an idea to make it default to true - I think you can do this in your Master Page's constructor of Page_Init method:

public SiteMaster() {
    ...
    ShowScriptManager = true;
    ...
}

// Or alternatively
private void Page_Init(object sender, EventArgs e) {
    ...
    ShowScriptManager = true;
    ...
}

Then, if you've set the MasterType in your content pages:

<%@ MasterType VirtualPath="~/path/to/master/page" %>

You just need to do something like this in content page's Page_Load:

Master.ShowScriptManager = false;
寂寞美少年 2024-08-02 05:50:38

您还可以将脚本管理器放入 ContentPlaceHolder 中,

<asp:ContentPlaceHolder ID="cph_ScriptManager" runat="server"></asp:ContentPlaceHolder>
    <asp:ScriptManager ID="ScriptManager" runat="server"></asp:ScriptManager>
</asp:ContentPlaceHolder>

并在要删除它的页面上,使用 asp:Content 标记指向它,它将从页面中删除它:

<asp:Content ID="content_SM_Overrride" ContentPlaceHolderID="cph_ScriptManager" runat="server">
<!-- ScriptManager Not Needed on this ASPX  -->
</asp:Content>

You could also put the script manager into a ContentPlaceHolder,

<asp:ContentPlaceHolder ID="cph_ScriptManager" runat="server"></asp:ContentPlaceHolder>
    <asp:ScriptManager ID="ScriptManager" runat="server"></asp:ScriptManager>
</asp:ContentPlaceHolder>

and on the pages you want to remove it , have a asp:Content tag point at it, and it will remove it from the page:

<asp:Content ID="content_SM_Overrride" ContentPlaceHolderID="cph_ScriptManager" runat="server">
<!-- ScriptManager Not Needed on this ASPX  -->
</asp:Content>
抹茶夏天i‖ 2024-08-02 05:50:38

对于最终到达这里但仍然无法使其与 UserControl 一起使用的任何人...

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

ScriptManager.AjaxFrameworkMode 属性

希望有帮助有人!

For anyone who ends up here and still can't get it to work with a UserControl...

If you are using .Net 4.0 you can use the new AjaxFrameworkMode property and set it to Disabled.

ScriptManager.AjaxFrameworkMode Property

Hope that helps someone!

jJeQQOZ5 2024-08-02 05:50:38

我会使用嵌套母版页。 一个基础母版,其中包含您的标记以及脚本管理器所在位置的额外内容占位符。 然后是嵌套母版的两个版本,一种带有脚本管理器,一种没有。 并且您的页面使用适当的嵌套母版页。

我将留在下面的文本中,以便注释有意义,但这不起作用...

这样怎么样:

- 在您的 webconfig 中放置一个应用程序设置,其中包含您不希望有脚本管理器的 URI 列表.
- 在主服务器的 page_init 事件处理程序中,获取该集合并测试当前页面请求是否在列表中。 如果是这样,请从 master 的控件集合中删除 scriptmanager。

即,在母版页代码后面:

Private Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
    If DirectCast(Page, System.Web.UI.Page).AppRelativeVirtualPath = "~/Test.aspx" Then
        Me.Controls.Remove(Me.FindControl("ScriptManager1"))
    End If
End Sub

注意:您正在做的事情存在很多危险。 如果您的母版页有任何更新面板,或者您要删除管理器的任何页面有它们,它们就会崩溃。 您可以在 masters init 中循环遍历 master 和页面的控制集合,并检查是否有任何更新面板。 不过,我不确定如果你找到它们你会做什么。 删除它们可能会删除其中的任何内容。 最好的情况是,您可以 1) 如果找到更新面板,则不删除脚本管理器,或者 2) 自定义错误。

I would use nested master pages. A base master that has your markup with an extra content place holder where the script manager would be. Then two versions of the nested master, one with a script manager and one without. And your pages use the appropriate nested master page.

I'm leaving in the text below so the comments make sense, but this doesn't work...

How about this:

-Put an appsetting in your webconfig with a list of URI's that you don't want to have a script manager.
-In the page_init event handler of the master, get that collection and test to see if the current page request is in the list. If so, remove the scriptmanager from the controls collection of the master.

ie, in the master page code behind:

Private Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
    If DirectCast(Page, System.Web.UI.Page).AppRelativeVirtualPath = "~/Test.aspx" Then
        Me.Controls.Remove(Me.FindControl("ScriptManager1"))
    End If
End Sub

Note: There is a lot of danger in what you're doing. If your master page has any update panels, or any of the pages you are removing the manager on have them, they will bomb out. You could loop through the control collection of the master and the page in the masters init and check for any update panels as well. Although, I'm not sure what you would do if you found them. Removing them would likely remove any content in them. At best, you could either 1) not remove the Script Manager if an update panel is found, or 2) Customize the error.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文