前台调用后台事件的方法

前台:

<html>
<head id="Head1" runat="server">
  <meta http-equiv="content-type" content="text/html;charset=gb2312" />
  <script src="../../js/jquery-1.3.2.js" type="text/javascript"></script>
</head>
<body>
<input type="button" value="调用Test" onclick="CallMethod('Test')"/><input type="button" value="调用Test1" 

onclick="CallMethod('Test1')"/><input type="button" value="调用Test2" onclick="CallMethod('Test2')"/>
<script type="text/javascript">
    function CallMethod(method){
        $.ajax(
  {
      type: "POST",
      url: "WebForm1.aspx",
      data: { method: method },
      success: function(msg) { alert(msg); },
      error: function() { alert('出错了'); }
  }
  )
    }

</script>
</body>
</html>

后台:

 protected void Page_Load(object sender, EventArgs e)
        {
            Response.Charset = "gb2312";
            if (Request.Form["method"] == "Test") Test();
            else if (Request.Form["method"] == "Test1") Test1();
            else if (Request.Form["method"] == "Test2") Test2();

            Response.Write("一般请求<br/>");


        }


        [System.Web.Services.WebMethod]
        public static string SayHello()
        {
            return "Hello Ajax!";
        }


        public void Test()
        {
            Response.Write("执行Test方法" + DateTime.Now);
            Response.End();//停止其他输出
        }
        public void Test1()
        {
            Response.Write("执行Test1方法" + DateTime.Now);
            Response.End();//停止其他输出
        }
        public void Test2()
        {
            Response.Write("执行Test2方法" + DateTime.Now);
            Response.End();//停止其他输出
        } 

原文地址:https://www.cnblogs.com/tongdengquan/p/6090582.html