代码改变世界

ajax 调用后台的方法

2009-06-27 15:30  AkingShoot  阅读(371)  评论(0编辑  收藏  举报

//前台的代码 

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods=true>
        </asp:ScriptManager>
        <script>
        (
        function scriptgetdate() {
           PageMethods.getdate(onsuccess);//通过PageMethod调用后台的方法
        }
        )();
        function onsuccess(message) {//回调函数
            $get("message").innerHTML= message;
        }
        </script>
   //后台的方法

 [System.Web.Services.WebMethod]//必须在方法的上面引用[System.web.services.WebMethod] 
    public static string getdate()
    {
        //return "Hello World!";
        return "<font color='red'>"+System.DateTime.Now.ToShortDateString()+"</font>";
    }