通过脚本调用:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>无标题页</title>
<mce:script type ="text/javascript"><!--
function userAdd()
{
var name = $get("txtName").value;
var pwd = $get("txtPwd").value;
UserService .UserAdd(name,pwd,userAddCallBack);
}
//回滚调用方法
function userAddCallBack(res)
{
alert(res);
}
// --></mce:script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="UserService.asmx" />
</Services>
</asp:ScriptManager>
</div>
<h2>Ajax调用Xml Web Service示例1</h2>
<div style="border: 1px solid Black; width: 50%; padding: 10px;">
<table class="style1">
<tr>
<td>
用户名:
</td>
<td>
<input id="txtName" type="text" />
</td>
</tr>
<tr>
<td>
密码:
</td>
<td>
<input id="txtPwd" type="password" />
</td>
</tr>
<tr>
<td>
</td>
<td>
<input id="Button2" type="button" value="提交" onclick="userAdd()" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
通过添加web引用调用:
调用本地解决方案中的WebService
添加新项->Web服务(WebService.asmx)
在WebService.cs中添加方法(简单加法)Add(int a,int b)
代码:
[WebMethod]
public int Add(int a,int b) {
return a+b;
}
保存
在WebPage(Default.aspx)中调用
先添加web引用->此解决方案中的Web服务->添加WebService.asmx引用(引用名;testWebService)
在Default.aspx中的添加TextBox1,TextBox2,label1,button1
在button1的触发事件中添加调用代码:
testWebService.WebService test=new testWebService();
int a=Convert.ToInt32(TextBox1.text);
int b=Convert.ToInt32(TextBox2.text);
int c;
c=test.Add(a,b);
label1.text=c.toString();