views:

1172

answers:

3

Hi

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?

A: 

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.

Tim Hoolihan
That may cause errors with pages containing update panels.
Kirtan
agreed, on pages that you plan on removing the script manager, you should not have a update panel. but that problem is inherit in his question: in other words, it is a problem with any page that removes the script manager, and that's what he's asking to do.
Tim Hoolihan
yes - I am looking how to remove the script manager. I havwe tried the followingScriptManager sm = (ScriptManager)Master.FindControl("sm"); sm.Visible = false;but that gives an error :The method or operation is not implemented.So the question is simply:How do you remove the script manager from a pageThere are no update panels or anything like that on the pages in question
Christo Fur
added sample above
Tim Hoolihan
thanks for that.It still renders the scriptrersource.axdI also tried Master.Controls.Remove(Master.FindControl("sm"));but that didnt help eitherhmmmm...back to the drawing board I think
Christo Fur
I see what you mean. I tried ScriptManager1.Scripts.Clear() in init, load, and pre_render and couldn't get the script to not show up on the page. I'll add another method above
Tim Hoolihan
+2  A: 

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;
Ian Oxley
Atta boy. Nice solution. I just spent 4 hours experimenting to come up with the same solution. I should have come here first!
Ian Patrick Hughes
A: 

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>
BigBlondeViking