热门:网页模板.net视频教程JQueryMVCjsonExtJs源码示例三级联动JQuery菜单
您现在的位置:.Net中文社区>> .Net编程>>正文内容

webservice实现jS将前台的参数传到后台

发布时间:2009年09月30日点击数: 未知

用asp.net ajax的话相当简单,比如把下拉列表框中选择项的value值传递给服务器端Web Service,处理后再把结果显示到某个 <div>里的例子:

1、给项目增加一个Web Service
MyService.cs

[System.Web.Script.Services.ScriptService]public
class MyService : System.Web.Services.WebService {    [WebMethod]    public
string GetString(string s) {        return
"Hello World "
+ s; //也就是给客户端传来的加个Hello World再返回给客户端。
    }    }

Default.aspx

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function callService(s) {            MyService.GetString(s, OnSuccess);        }        function OnSuccess(result) {            $get('result').innerHTML = result;        }    </script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
<Services>
<asp:ServiceReference Path="~/MyService.asmx"
/>
</Services>
</asp:ScriptManager>
<div>
<select id="sel">
<option value="1">商品一</option>
<option value="2">商品二</option>
<option value="3">商品三</option>
</select>
<input type="button" onclick="callService($get('sel').value);" value="Go!"
/>
</div>
<div id="result">
</div>
</form>
</body>
</html>
 

 

本站热点业务

更多模板/案例展示

关于我们 | 联系我们 | 团队日志 | 网站地图 | 网站合作