日期:2014-05-17  浏览次数:20624 次

在js里面调用aspx.cs文件里的方法(不使用webservers)
在js代码中调用aspx.cs文件中的方法,不使用webservers
js

------解决方案--------------------

<%@ Page Language="C#"%>
<%@ Implements Interface="System.Web.UI.ICallbackEventHandler" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
        #region 回调
        private string Result;
        public void RaiseCallbackEvent(string eventArgument)
        {
            Result = "执行到了 " + eventArgument + " 哈哈";
        }
        public string GetCallbackResult()
        {
            return Result;
        }
        #endregion
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script type="text/javascript">
 //回调方法
 function CallServerFunction(Arg){
    <%= ClientScript.GetCallbackEventReference(this,"Arg","ReceiveServerData","null") %>
 }
  //返回结果
function ReceiveServerData(result){
alert(result);
}
 </script>
</head>
<body>
    <form id="form1" runat="server">
        <input id="Button1" type="button" value="button" onclick="CallServerFunction(new Date().toLocaleTimeString())" />
    </form>
</body>
</html>

给你个例子,