Script Manager Register Startup Script only runs script on initial Page Load When run through a Master Page Function

1.8k Views Asked by At

Edit:

I checked the page via developer tools and both of them put a script block with the function calls in the script block. The one running through the master page places the script block higher up in the page, a few lines from the top of the form. The ones called from the page place them with other script blocks near the end of the form element. I'm not sure if that means anything to anyone or not, but it seemed relevant to me.


Original Question

I have a multi page application in which I use the RegisterStartupScript method of the ScriptManager on multiple occasions on each page. We have a Master page, so I decided to put a Public function that just takes the name and optionally different script text and registers the startup scripts there so that way we can have a smaller, simpler function call for startup scripts and one that is accessible from every page that uses the master page. Here's the function:

Public Sub RunStartupScript( ByVal name As String, ByVal Optional script As String = "")
    If script = "" Then
        script = name & "();"
    End If
    ScriptManager.RegisterStartupScript(Me, [GetType](), name, script, True)
End Sub

I have the script manager in the aspx of the master page as well, and when using this function it does work and run the script, but only for the initial page load. Any subsequent async postbacks do not run any scripts that have already been run.

Master.RunStartupScript("SomePredefinedJavaScriptFunction")

If I take this exact function code and place it in the code behind of the specific page, it works just fine and will run on any postback. Why does this not work right when the function is owned by the master page?

Any thoughts?

2

There are 2 best solutions below

3
G. Stoynev On

You say I have the script manager in the aspx of the master page as well - does that as wel mean that you have it in the content pages? Per MSDN

Using the ScriptManager Control with Master Pages, User Controls, and Other Child Components A page can contain only one ScriptManager control in its hierarchy. To register services and scripts for nested pages, user controls, or components when the parent page already has a ScriptManager control, use the ScriptManagerProxy control. For more information, see Using the ASP.NET UpdatePanel Control with Master Pages

While this doesn't answer why your approach doesn't work, I think it suggest that you should consider using the proxy object on content pages.

EDIT

Based on your comment, you should also consider accessing the script manager on content pages not by exposing your own property, but by ScriptManager's GetCurrent method

I am curious if doing it this way provides any improvement:

    Public Sub RunStartupScript(System.Web.UI.ScriptManager sm, ByVal name As String, ByVal Optional script As String = "")
        If script = "" Then
            script = name & "();"
        End If
        sm.RegisterStartupScript(Page, Page.GetType(), name, script, True)
    End Sub

and call Master.RunStartupScript(ScriptManager.GetCurrent(...), "SomePredefinedJavaScriptFunction")

0
Daniel Ecker On

So it turns out it was a pretty simple mistake (as these things so often are), but hard to see. I forgot that Me doesn't refer to the Page in a Master Page file, but instead refers to the Master type. Changing the Me to Page fixed the issue.

    Public Sub RunStartupScript( ByVal name As String, ByVal Optional script As String = "")
        If script = "" Then
            script = name & "();"
        End If
        ScriptManager.RegisterStartupScript(Page, Page.GetType(), name, script, True)
    End Sub

Thanks for the other answers and comments. Looking at those helped me to go back through and retrace my steps to find the issue.