Disable ScriptManager on certain pages

杀马特。学长 韩版系。学妹 提交于 2019-12-19 09:11:29

问题


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?


回答1:


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;



回答2:


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>



回答3:


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!




回答4:


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.



来源:https://stackoverflow.com/questions/1057905/disable-scriptmanager-on-certain-pages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!