zoukankan      html  css  js  c++  java
  • ASP.net AJAX 调用PageMethods实例(javascript调用服务器页面方法)

    PageMethods.静态方法名(参数列表,成功执行的回调函数,失败的回调函数,上下文对象)

    1、新建网站,选择Asp.net Ajax Enabled Web Site 模板

    2、 <asp:scriptManager ID="scriptManager1" runat="server"/>中增加 EnablePageMethods="True" ,以启用PageMethods
    3、编写服务器端方法代码

    [System.Web.Services.WebMethod]
    public static string GetServerTime()
    {
          return DateTime.Now.ToString();
    }

    请注意:a、[System.Web.Services.WebMethod]是必须的,相当于ajax.net中的[ajax.method],b、方法前面要上static

    4、页面设计,内容如下
          <div>
    <input type="button" value="服务器时间" id="btnGetServerTime" onclick="return btnGetServerTime_onclick()" />
    <span id="result" />
    </div>


    5、编写客户端javascript
       <script language="javascript" type="text/javascript">
    // <!CDATA[

    function btnGetServerTime_onclick() {
    PageMethods.GetServerTime(cb_getServerTime);
    }

    function cb_getServerTime(result) {
    document.getElementById("result").innerHTML = result;
    }

    6、执行程序,单击"服务器时间" 按钮就可返回服务器的时间并显示在result中。

    附:

    一、客户端代完整代码

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
         <title>Untitled Page</title>
         <script language="javascript" type="text/javascript">
    // <!CDATA[

    function btnGetServerTime_onclick() {
    PageMethods.GetServerTime(cb_getServerTime);
    }

    function cb_getServerTime(result) {
    document.getElementById("result").innerHTML = result;
    }

    // ]]>
    </script>

    </head>
    <body>
         <form id="form1" runat="server">
             <asp:scriptManager ID="scriptManager1" runat="server" EnablePageMethods="True" />
            <div>
    <input type="button" value="服务器时间" id="btnGetServerTime" onclick="return btnGetServerTime_onclick()" />
    <span id="result" />
    </div>

         </form>
    </body>
    </html>

    二、服务器端完整代码

    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;

    public partial class _Default : System.Web.UI.Page
    {
         [System.Web.Services.WebMethod]
         public static string GetServerTime()
         {
             return DateTime.Now.ToString();
         }
    }

  • 相关阅读:
    AngularJS 1.x系列:AngularJS过滤器(4)
    AngularJS 1.x系列:AngularJS控制器(3)
    AngularJS 1.x系列:AngularJS简介及第一个应用(2)
    AngularJS 1.x系列:Node.js安装及npm常用命令(1)
    MySQL系列:索引基本操作(4)
    基于 Mathematica 的机器人仿真环境(机械臂篇)[转]
    使用耳切法将多边形三角化【转】
    asp.net调用非托管dll,无法加载 DLL,找不到指定模块解决方法。【转】
    ASP.NET与非托管DLL的那些事儿【转+增】
    cesium常用设置【转】
  • 原文地址:https://www.cnblogs.com/yeagen/p/1338075.html
Copyright © 2011-2022 走看看