Ajax使用PageMethods调用后台方法
2012-06-27 17:44
351 查看
很多东西都不会,从头学起;这里贴使用PageMethods调用后台方法
在后台上写两个方法,一个是有参,一个无参
[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
public static object GetStatus()
{
return System.DateTime.Now.ToString();
}
[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
public static object SetName(string firstName, string lastName)
{
return firstName + " " + lastName;
}
在脚本上进行调用
<script language="javascript" type="text/javascript">
/*
注意事项:
(a)需要调用的服务器端方法必须以System.Web.Services.WebMethod特性进行标记
(b)需要调用的服务器端方法必须为公共静态方法
(c)需要调用的服务器端方法应写在.aspx页面(或对应的后台代码文件)中,不应写在用户控件中
*/
window.setInterval(function () {
PageMethods.GetStatus(function (result) {
if (result) {
alert(result);//弹出当前时间
}
});
}, 3000);
window.onload = function () {
PageMethods.SetName("zhang", "jinshan", function (result) {
alert(result);//弹出姓名
});
}
//以下的写法是错误的:直接
PageMethods.SetName("zhang", "jinshan", function (result) {
alert(result);//弹出姓名
});
</script>
在表单中需要添加 EnablePageMethods="true"
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
在后台上写两个方法,一个是有参,一个无参
[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
public static object GetStatus()
{
return System.DateTime.Now.ToString();
}
[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
public static object SetName(string firstName, string lastName)
{
return firstName + " " + lastName;
}
在脚本上进行调用
<script language="javascript" type="text/javascript">
/*
注意事项:
(a)需要调用的服务器端方法必须以System.Web.Services.WebMethod特性进行标记
(b)需要调用的服务器端方法必须为公共静态方法
(c)需要调用的服务器端方法应写在.aspx页面(或对应的后台代码文件)中,不应写在用户控件中
*/
window.setInterval(function () {
PageMethods.GetStatus(function (result) {
if (result) {
alert(result);//弹出当前时间
}
});
}, 3000);
window.onload = function () {
PageMethods.SetName("zhang", "jinshan", function (result) {
alert(result);//弹出姓名
});
}
//以下的写法是错误的:直接
PageMethods.SetName("zhang", "jinshan", function (result) {
alert(result);//弹出姓名
});
</script>
在表单中需要添加 EnablePageMethods="true"
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
相关文章推荐
- ASP.Net Ajax应用初步:使用AJAX直接调用后台方法
- 微软ASP.Net Ajax:使用AJAX直接调用后台页面类方法
- jquery 使用ajax调用c#后台方法
- jquery 使用ajax调用c#后台方法
- 微软ASP.Net Ajax:使用AJAX直接调用后台页面类方法
- 使用AJAX直接调用后台方法
- 使用ajax,javascript中调用后台的方法
- .net 使用$.ajax实现从前台调用后台方法(包含静态方法和非静态方法调用)
- 使用AjaxPro,js调用后台方法
- 微软ASP.Net Ajax:使用AJAX直接调用后台页面类方法
- C#前台js里面使用ajax调用后台aspx.cs方法
- [转]javascript 异步调用 后台.cs里的方法 PageMethods如何使用
- 在js 中使用ajax 调用后台代码方法,解析返回值
- jQuery之异步Ajax请求使用 通过传递参数来调用后台方法返回状态
- Java后台框架篇--Struct2之使用Ajax调用Action方法并返回值
- 微软ASP.Net Ajax:使用AJAX直接调用后台页面类方法
- Ajax 1.0 中使用web控件调用后台方法的用法.
- 使用asp.net ajax的PageMethods来让客户端代码调用服务器端的方法
- java 中使用ajax调用后台方法注意事项
- 初试.net使用ajax调用后台方法