客户端访问WebService和PageMethod

客户端访问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;
        }

上一篇:数据结构 之 二叉堆(Heap)


下一篇:[Spring框架]Spring IOC的原理及详解。