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

喜夏-厌秋 提交于 2019-12-10 16:37:57

问题


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


回答1:


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>



回答2:


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...




回答3:


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" />



来源:https://stackoverflow.com/questions/7062286/sys-webforms-pagerequestmanager-is-null-or-not-an-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!