自强不息,止于至善

身无半文,心忧天下;手释万卷,神交古人
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

访问WebService复杂数据类型使用

Posted on 2007-10-28 13:37  L.Zhang  阅读(416)  评论(0)    收藏  举报

//前台代码

    <form id="form1" runat="server">
        
<asp:ScriptManager ID="ScriptManager1" runat="server">
            
<Services>
                
<asp:ServiceReference Path="ComplexType.asmx" />
            
</Services>
        
</asp:ScriptManager>
        
        
<input type="button" value="Double Salary" onclick="doubleSalary()" />
        
<input type="button" value="Reverse" onclick="reverse([1, 2, 3, 4, 5])" />
        
<input type="button" value="Get Employees" onclick="getEmployees()" />
        
        
<script language="javascript" type="text/javascript">
            
//传递对象
            function doubleSalary()
            {
                
var employee = new Object();
                employee.FirstName 
= "Jeffrey";
                employee.LastName 
= "Zhao";
                employee.Salary 
= 1000;
                
                ComplexType.DoubleSalary(employee, doubleSalarySucceeded);
            }
            
function doubleSalarySucceeded(result)
            {
                
var message = String.format(
                    
"First Name: {0}\nLast Name: {1}\nFull Name: {2}\nSalary: {3}",
                    result.FirstName,
                    result.LastName,
                    result.FullName,
                    result.Salary);
                    
                alert(message);
            }
            
//数组倒序
            function reverse(array)
            {
                ComplexType.Reverse(array, 
function(result){alert(result);});
            }
            
//获取对象数组
            function getEmployees()
            {
                ComplexType.GetEmployees(getEmployeesSucceeded);
            }
            
            
function getEmployeesSucceeded(result)
            {
                
for (var name in result)
                {
                    alert(name 
+ "" + result[name].Salary)
                }
            }
            
        
</script>
    
</form>
//WebService
    [WebMethod]
    
public Employee DoubleSalary(Employee employee)
    {
        employee.Salary 
*= 2;
        
return employee; 
    }

    [WebMethod]
    
public List<int> Reverse(List<int> list)
    {
        list.Reverse();
        
return list;
    }

    [WebMethod]
    
public IDictionary<string, Employee> GetEmployees()
    {
        Dictionary
<string, Employee> result = new Dictionary<string, Employee>();

        Employee emp1 
= new Employee();
        emp1.FirstName 
= "Jeffrey";
        emp1.LastName 
= "Zhao";
        emp1.Salary 
= 1000;
        result[emp1.FullName] 
= emp1;

        Employee emp2 
= new Employee();
        emp2.FirstName 
= "Tom";
        emp2.LastName 
= "Chen";
        emp2.Salary 
= 2000;
        result[emp2.FullName] 
= emp2;

        
return result;
    }
//实体类
public class Employee
{
    
public string FirstName;

    
public string LastName;

    
public int Salary;

    
public string FullName
    {
        
get
        {
            
return this.FirstName + " " + this.LastName;
        }
    }
}