SharePoint 2010 的 Webpart 中的错误"PageMethods is undefined"



我的sharepoint 2010 Web部件中出现PageMethods未定义的javascript错误。基本上,我已经创建了显示谷歌地图的Web部件。单击谷歌地图的标记后,我想调用服务器端事件。我已经实现了相同的jquery。我正在使用PageMethods来调用服务器方法。下面是我从一些博客网站上获取的代码。我还在页面上添加了scriptmanager标记,并将EnablePageMethods属性设置为true。

脚本块:

<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应用程序中不起作用。有人能帮我吗。

对于Page方法,Web方法需要在.aspx.cs文件中。

在SharePoint中(如果使用发布页面布局),在页面的cs中编写web方法。

但是,如果您使用的是Web部件页布局,那么它将不起作用。

相关内容

最新更新