客户端访问WebService

客户端访问WebService和后台访问WebService没什么不同,注意的地方是要在ScriptManager中添加

<Services>
             <asp:ServiceReference  Path=""/>
          </Services>

path 指向服务文件,使用web服务文件时在js函数中调用相关方法。

例子:

客户端:

<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
          <Services>
             <asp:ServiceReference Path="~/FullType.asmx"/>
          </Services>
        </asp:ScriptManager>
        <input type="button" value="salarydouble" onclick="salaryDouble()" />
        <input type="button" value="Reversal" onclick="ReversalList([1,2,3,4,5])" />
        <input type="button" value="getEmployee" onclick="GetEmployees()" />

</div>
    <script type="text/javascript">
        function salaryDouble() {
            var employee = new Object();
            employee.FirstName = "Dan";
            employee.LastName = "Che";
            employee.Salary = 1000;

Demo1MethodChongZai.FullType.SalaryDouble(employee, SalarySuccessfully);
        }
        function SalarySuccessfully(result) {
            alert(String.format("FirstName:{0}\nLastName:{1}\nSalary:{2}\nFullName:{3}\n", result.FirstName, result.LastName, result.Salary, result.FullName));
        }
        function ReversalList(list) {
            Demo1MethodChongZai.FullType.reversalList(list,RversalSuccessfully);
        }
        function RversalSuccessfully(result) {
            alert(result);
        }
        function GetEmployees() {
            Demo1MethodChongZai.FullType.GetEmployee(employeeSuccessfully);
        }
        function employeeSuccessfully(result) {
            for (var i in result) {
                alert(String.format("result[{0}]:\nFirstName:{1}\nLastName:{2}\nSalary:{3}\nFullName:{4}",i,result[i].FirstName,result[i].LastName,result[i].Salary,result[i].FullName));
              
            }
        }
    </script>
    </form>
</body>

Web服务文件:

/// <summary>
    /// FullType 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ScriptService]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
    // [System.Web.Script.Services.ScriptService]
    public class FullType : System.Web.Services.WebService
    {

[WebMethod]
        public Employee SalaryDouble(Employee employee)
        {
            employee.Salary *= 2;
            return employee;
           
        }
        [WebMethod]
        public List<int> reversalList(List<int> list)
        {
            list.Reverse();
            return list;
        }
        [WebMethod]
        public IDictionary<string, Employee> GetEmployee()
        {
            Dictionary<string, Employee> result = new Dictionary<string, Employee>();
            Employee employee = new Employee();
            employee.FirstName = "Dan";
            employee.LastName = "Che";
            employee.Salary = 2000;
            result[employee.FullName] = employee;

Employee employee2 = new Employee();
            employee2.FirstName = "Tan";
            employee2.LastName = "Meng";
            employee2.Salary = 2000;
            result[employee2.FullName]=employee2;

return result;
           
           
         
        }
       
    }

注意:客户端访问WebService在性能方面比UpdatePanel好,UpdatePanel在性能上没有什么优势,它在postback给服务器的是整个页面,但回传的却是页面的一部分,强烈推荐使用客户端访问WebService方法。

客户端访问PageMethod
在服务器端:

1.只能在aspx.cs页面中定义
            2.只能是public static 方法
            3.要使用[WebMethod]标记
           
客户端:1.要将ScriptManager的EnablePageMethods属性改为true
           2.要页面javascrpt函数中使用PangeMethods来调用后台方法

例子:

客户端:

<form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
        </asp:ScriptManager>
        <input type="button" id="btnGetTime" value="getTime" onclick="getTime()" />
    </div>
    <script type="text/javascript">
        function getTime() {
            PageMethods.getDateTime(huidiao);
        }
        function huidiao(result) {
            alert(result);
        }
    </script>
    </form>

后台:

[WebMethod]
        public static DateTime getDateTime()
        {
            return DateTime.UtcNow;
        }

客户端访问WebService和PageMethod的更多相关文章

  1. C++客户端访问WebService VS2008

    VS2008及之后的版本已经不支持使用C++开发WEBService服务了,如果要在VS上开发WEBService,需要使用C#开发语言. 一.gSOAP简介 gSOAP编译工具提供了一个基于SOAP ...

  2. 问题-XE8客户端访问Webservice时报“no selected dom vendor”

    问题现象:XE8做的客户端访问XE8做的Webservice时,客户端报“no selected dom vendor”. 问题原因:原因不明,应该是用到了XML转换等方法吧.有高手了解的,请M我. ...

  3. axis客户端循环访问webservice的时候只发送了几条数据就断开了的问题

    原因 axis客户端访问webservice默认是使用http1.0版本的,这个版本的http不能保持长连接,应该换成http1.1版本 具体修改步骤: <?xml version=" ...

  4. ASP.NET MVC中使用ASP.NET AJAX异步访问WebService

    使用过ASP.NET AJAX的朋友都知道,怎么通过ASP.NET AJAX在客户端访问WebService,其实在ASP.NET MVC中使用ASP.NET AJAX异步访问WebService 也 ...

  5. 【转】Spring的WebServiceTemplate访问WebService的方法及其本质原理

    WebService客户端调用的本质就是将SAOP格式的XML通过通信协议发送到WebService的服务器端,然后接收服务器端返回的XML. 本文简单介绍一下如何通过Spring提供的WebServ ...

  6. ajax——client访问webservice基本用法

    学前aps.net当我学会了使用服务器端的访问webservice方法,然后实现一个样本:web server模拟网上购物,今天学习asp.net ajax的时候学习到了client直接訪问webse ...

  7. php——SoapClient访问webservice

    原文:php--SoapClient访问webservice 通过SoapClient调用天气预报 <?phpheader ( "Content-Type: text/html; ch ...

  8. SQL Server 2008 R2 根据WSDL访问WebService

    参考网站:WebService学习整理(一)——客户端三种调用方式整理 自我概括: WebService 通过HTTP通讯,数据以XML格式传输使两个系统进行数据交互 SOAP 是访问协议(注明访问W ...

  9. Android局域网访问webservice以及其中的一些问题

    应老师的要求,要做个安卓app,实现备份app上的数据到服务器上的mongodb上,网上搜了下相关的实现方式.利用webservice技术,具体来说就是客户端直接调用服务器端的接口.之前从来没接触这玩 ...

随机推荐

  1. 7. Android框架和工具之 android-percent-support-lib-sample(百分比支持)

    1. android-percent-support-lib-sample介绍: 谷歌最新的百分比布局库的示例项目.其实LinearLayout的layout_weight也能实现百分比效果,不过这个 ...

  2. Oracle 常用的SQL语法和数据对象

    一.数据控制语句 (DML) 部分 1.INSERT (往数据表里插入记录的语句) INSERT INTO 表名(字段名1, 字段名2, ……) VALUES ( 值1, 值2, ……);  INSE ...

  3. java产生不重复的随机数

    /** *产生9位不同的随机数 */ private String getRandomString(){ StringBuffer sb = new StringBuffer(); for(int i ...

  4. Kinect For Windows V2开发日志五:使用OpenCV显示彩色图像及红外图像

    彩色图像 #include <iostream> #include <Kinect.h> #include <opencv2\highgui.hpp> using ...

  5. SPOJ 7758. Growing Strings AC自动机DP

    Growing Strings 题目:给出n个字符串,问最多能够选出多少个串组成序列,并满足前一个字符串是后一个字符串的子串. 分析: AC自动机经典水题... 考虑每个节点结尾时,他能够选出最多的串 ...

  6. latex之设置字体大小

    \tiny\scriptsize\footnotesize\small\normalsize\large\Large\LARGE\huge\Huge

  7. spring项目中如何添加定时器以及在定时器中自动生成sprng注入对象

    最近做了一个java的项目,部门领导给了一套代码让我尽快掌握,说心里话本人真心不喜欢java的这种项目方式,各种配置各种xml文件简直头都大了,下面就将我遇到的其中一个我认为是坑的地方整理出来,希望能 ...

  8. 如何使用OpenShare部署和运营企业门户

    如何使用OpenShare部署和运营企业门户 这篇Blog是偏向企业内整体门户部署和运营的指南,是偏向整体管理和规划的,并不是针对终端用户的OpenShare软件操作手册,具体的操作可以上优酷看相关视 ...

  9. AngularJS学习小结

    在刚学习AngularJS的时候觉得好像挺简单的,看见老师每次用很少的代码就做出用源生代码或者JQuery要用多行代码才做出的效果的时候觉得好像思路很简单,也很好写就写出来了,但是等到我们自己做的时候 ...

  10. link_mysql的php版

    <?php $str_sql_read="select count(*) as num from userinfo"; $str_sql_del="delete f ...