rocker
rocker

Reputation: 41

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

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

Upvotes: 4

Views: 19600

Answers (3)

Nastya Kholodova
Nastya Kholodova

Reputation: 1321

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>

Upvotes: 5

Darren J. McLeod
Darren J. McLeod

Reputation: 146

I had this problem as well. For me it was due to a web farm and missing machinekey entry in the web.config.

<system.web>
 <machineKey validationKey="D61B3C89CB33A2F1422FF158AFF7320E8DB8CB5CDA1742572A487D94018787EF42682B202B746511891C1BAF47F8D25C07F6C39A104696DB51F17C529AD3CABE"
   decryptionKey="FBF50941F22D6A3B229EA593F24C41203DA6837F1122EF17" />

Upvotes: 0

Waqas
Waqas

Reputation: 6802

It seems like your JavaScript block is executing before ASP.net Ajax has loaded, try placing this at the bottom on your page or after <form> tags...

Upvotes: 2

Related Questions