2012-03-29 51 views
1

我在我的SharePoint 2010 webpart中收到PageMethods未定義的JavaScript錯誤。 基本上,我創建了顯示谷歌地圖的webpart。點擊谷歌地圖的標記後,我想調用服務器端事件。我已經實現了相同的jQuery。我正在使用PageMethods來調用服務器方法。以下是我從一些博客網站獲取的代碼。我還在頁面上添加了scriptmanager標記,並將EnablePageMethods屬性設置爲true。在Sharepoint 2010的Webpart中出現「PageMethods is undefined」錯誤

腳本塊:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script> 

<script type="text/javascript"> 
    $(document).ready(function() { 
     // Initialization        
     $("#ajax_loading_image").hide(); 
     // mousemove event 
     $().mousemove(function (e) { 
      window.status = e.pageX + ', ' + e.pageY; 
     }); 
     // hook up the click event    
     $("#execute_page_method").click(function() { 

      $("#message").text("I'm working..."); 
      $("#ajax_loading_image").show("fast"); 
      $("#execute_page_method").hide("fast"); 

      // Call some page method... 
      PageMethods.ProperMethodName("string", function (result, userContext, methodName) { 

       $("#ajax_loading_image").hide("slow"); 
       $("#execute_page_method").show("slow"); 

       if (result.Success == true) { 
        alert("true"); 
        $("body").css("background", "#CAFFD8"); 
       } 
       else { 
        $("body").css("background", "#CAFFFF"); 
       } 

       $("#message").text("This took me " + result.Time + " milliseconds... "); 

      }); 
      return false; 
     }); 
    }); 
</script> 

<div> 
    <a id="execute_page_method" href="http://jquery.com/">Click!</a> 
</div> 

Code Behind events: 
public class MethodReturnedValue 
     { 
      public int Time { get; set; } 
      public bool Success { get; set; } 
     } 

     [WebMethod(true)] 
     public static MethodReturnedValue ProperMethodName(string param) 
     { 
      Random random = new Random(); 
      MethodReturnedValue retVal = new MethodReturnedValue(); 
      retVal.Time = random.Next(5000); 
      Thread.Sleep(retVal.Time); 
      if (random.Next() % 2 == 0) 
      { 
       retVal.Success = true; ; 
      } 
      else 
      { 
       retVal.Success = false; 
      } 
      return retVal; 
     } 

相同的代碼是在我的asp.net器件的應用工作正常,並在我的SharePoint應用程序無法正常工作。 任何人都可以請幫我在這裏。

回答

0

對於Page方法,Web方法需要位於.aspx.cs文件中。

在SharePoint中(如果使用發佈頁面佈局),請在頁面的cs中編寫web方法。

但是,如果您正在使用Web部件頁面佈局,那麼它將無法工作。

相關問題