views:

373

answers:

2

What causes the following pageLoad to not execute in a master page? The first alert executes as expected, but the pageLoad function is not being invoked. How can I use the function within a master/content page?

<script type="text/javascript">
        alert('test');
        function pageLoad(sender, args) {
            alert('pageLoad');
        }    
</script>
+1  A: 

Window Load Event

window.onload = function() {
    alert('pageLoad'); 
}

Self Executing Function

(function() {
    alert('pageLoad'); 
})();
ChaosPandion
I was using the implicit pageLoad function because it was my understanding that window.onload would not be called on ajax postbacks (via updatepanel). Is that not the case?
Jeremy
+1  A: 

I'm assuming a script manager control is present in your master page? I'm not sure if the client-side pageLoad method is only called on the initial page load. I do know that if you're looking for ajax postback page load hooks, try the client-side PageRequestManager. MAke sure that the code below is place inside the script manager.

    <asp:ScriptManager ID="ScriptManager1" runat="server" />

    <script type="text/javascript" >
    (function() {
        var prm = Sys.WebForms.PageRequestManager.getInstance();

        if (prm)
        {
            prm.add_endRequest(
            function (sender, args) {            
                // do your stuff

                // Handle a weird occurence where an error is returned but the error code is 0, i.e. no error.
                if(args.get_error() && args.get_error().name === 'Sys.WebForms.PageRequestManagerServerErrorException')
                {
                    args.set_errorHandled(args._error.httpStatusCode == 0);
                }
            });
        }
    })();
    </script>

<%-- Other markup --%>
</asp:ScriptManager>
nickyt
You assume wrong. I didn't realize pageLoad was a part of asp.net ajax, so didn't have the scriptmanager on the master page. Thanks.
Jeremy