日期:2014-05-18 浏览次数:20380 次
public class MyClass { public MyClass() { // //TODO: 在此处添加构造函数逻辑 // } private string name; public string Name { get { return name; } set { name = value; } } private int age; public int Age { get { return age; } set { age = value; } } }
[System.Web.Script.Services.ScriptService] public class MyWebService : System.Web.Services.WebService { public MyWebService () { //如果使用设计的组件,请取消注释以下行 //InitializeComponent(); } [WebMethod] public MyClass GetClass(MyClass cls) { cls.Name = cls.Name + " processed."; cls.Age = cls.Age++; return cls; } }
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>无标题页</title> <script language="javascript" type="text/javascript"> // <!CDATA[ function ButtonCallWebService_onclick() { var cls = new MyClass(); cls.Name = $get('TextName').value; cls.Age = $get('TextAge').value; MyWebService.GetClass(cls, OnSuccess); } function OnSuccess(cls) { alert(cls.Name + "," + cls.Age); } // ]]> </script> </head> <body> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server"> <Services> <asp:ServiceReference Path="~/MyWebService.asmx" /> </Services> </asp:ScriptManager> <div> name:<input id="TextName" type="text" /><br /> age: <input id="TextAge" type="text" /><br /> <input id="ButtonCallWebService" type="button" value="Call" onclick="return ButtonCallWebService_onclick()" /> </div> </form> </body> </html>