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?
You say
I have the script manager in the aspx of the master page as well- does thatas welmean that you have it in the content pages? Per MSDNWhile this doesn't answer why your approach doesn't work, I think it suggest that you should consider using the
proxyobject 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
GetCurrentmethodI am curious if doing it this way provides any improvement:
and call
Master.RunStartupScript(ScriptManager.GetCurrent(...), "SomePredefinedJavaScriptFunction")