'Sys.WebForms.PageRequestManager' is null or not an object

rocker picture rocker · Aug 15, 2011 · Viewed 18k times · Source

Hi I have an aspx page in which i have the following code

  <asp:ScriptManager ID="scriptManager" runat="server" AsyncPostBackTimeout="500" EnablePageMethods="true">
            </asp:ScriptManager>

            <script type="text/javascript">
          Sys.Application.add_init(BeginRequestHandler);
          Sys.Application.add_init(EndRequestHandler);

          Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequestHandler);
          Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
          function BeginRequestHandler(sender, args) {
              AsynProcessing('block', 'AlertDiv', 'ProcessingImage');
          }
          function EndRequestHandler(sender, args) {
              AsynProcessing('none', 'AlertDiv', '');
          }
          function AsynProcessing(visstring, elem, img) {
              var adiv = $get(elem);
              adiv.style.display = visstring;
              adiv.image = img;
          }

But the page is throwing a javascrip error as 'Sys.WebForms.PageRequestManager' is null or not an object. I have placed the below the scriptmanager tag. I even ried adding

<xhtmlConformance  mode="Transitional"/>

in the section of web.config.But still getting the same error.
Any help is much appreciated. Thanks in advance

Answer

Nastya Kholodova picture Nastya Kholodova · Aug 15, 2011

Wrap your handlers with this code, in order to wait for all nessesary scripts were loaded, before calling Sys.WebForms.PageRequestManager

Sys.Application.add_init(function(){ ... your code ....}

http://msdn.microsoft.com/en-us/library/bb397532.aspx

EDIT:The reason of error at this line Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequestHandl‌​er) is scripts havn't been loaded yet, so if you want handling of an asynchronous postback, you have to write something like this:

Sys.Application.add_init(function(){ 
    Sys.WebForms
       .PageRequestManager
       .getInstance()
       .add_beginRequest(BeginRequestHandler)
});

What does it mean in plain English? Wait until all scripts have been loaded (including Sys.WebForms namespace) and subscribe to event beginRequest You script block should be like this:

<script type="text/javascript">
    Sys.Application.add_init(function () {
        Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequestHandler);
    });
    Sys.Application.add_init(function () {
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
    });

    function BeginRequestHandler(sender, args) {
        AsynProcessing('block', 'AlertDiv', 'ProcessingImage');
    }
    function EndRequestHandler(sender, args) {
        AsynProcessing('none', 'AlertDiv', '');
    }
    function AsynProcessing(visstring, elem, img) {
         var adiv = $get(elem);
         adiv.style.display = visstring;
        adiv.image = img;
    }  
</script>