asp.net ajax框架使用技巧之一
2007-12-13 13:20
429 查看
一、改变服务器端方法在客户端相对应的方法名称
在开发中,可能会遇到这样的问题:服务器端公开了两个方法,其中一个方法是对另一个方法的重载。也就是有这两个方法的名称是相同的,只是方法签名不一样。此时,我们会发现,尽管服务器公开了两个方法,但我们只能使用其中的某一个,当查看自动生成的JS代理时,你会发现,页面中只生成了一个方法的代理。为什么会有这样问题?
asp.net ajax框架在为服务器方法生成代理时,是以JSON字典的方式来存储方法信息的,方法名称为字典的Key,Value为方法的其它信息。当遇到两个方法名相同的方法时(即Key相同),最后一个对相同key的操作会覆盖前面对这个Key的操作,所以当生成代理是,只会有一个方法的代理会被生成。
解决这种问题的方法比较简单,即编写服务器端方法时,在WebMethod的构造器中指定将要被生成到客户端的相应方法名称即可,即
[System.Web.Services.WebMethod]
public static int GetRandom()
{
Random random = new Random(DateTime.Now.Millisecond);
return random.Next();
}
[System.Web.Services.WebMethod(MessageName="getRangRandom")]
public static int GetRandom(int begin,int end)
{
Random random = new Random(DateTime.Now.Millisecond);
return random.Next(begin,end);
}
下面是一个完整的示例:
<%@ Page Language="C#" %>
<%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
Namespace="System.Web.UI" TagPrefix="asp" %>
[System.Web.Services.WebMethod]
public static int GetRandom()
{
Random random = new Random(DateTime.Now.Millisecond);
return random.Next();
}
[System.Web.Services.WebMethod(MessageName="getRangRandom")]
public static int GetRandom(int begin,int end)
{
Random random = new Random(DateTime.Now.Millisecond);
return random.Next(begin,end);
}
改变服务器在客户端对应的方法名称
PageMethods.set_timeout(5000);
PageMethods.set_defaultFailedCallback="OnFailed";
function getRandom(begin,end)
{
if ( arguments.length != 2 )
{
PageMethods.GetRandom(OnSucceed);
}
else
{
PageMethods.getRangRandom(begin,end,OnSucceed);
}
}
function OnSucceed(Message)
{
alert(Message);
}
function OnFailed(ErrorHander)
{
var ErrorMessage = '是否超时'+ErrorHander.get_timedOut()+'/n错误信息为:' + ErrorHander.get_message();
ErrorMessage = ErrorMessage + '/n异常名称为:' + ErrorHander.get_exceptionType() + '/n错误位置为:' + ErrorHander.get_stackTrace();
alert(ErrorMessage);
}
二、改变客户端访问服务器端的方式
默认情况下,在asp.net ajax中客户端访问服务器端时使用的是HTTP POST方式,如果您想使用HTTP GET的方式来访问服务器端,即可以这样做:
[System.Web.Services.WebMethod(MessageName="getRangRandom")]
[System.Web.Script.Services.ScriptMethod(UseHttpGet=true)]
public static int GetRandom(int begin,int end)
{
Random random = new Random(DateTime.Now.Millisecond);
return random.Next(begin,end);
}
这样,当客户端在调用GetRandom(int begin,int end)方法时,就是以HTTP GET的方式来调用。
三、在asp.net ajax中使用session
[System.Web.Services.WebMethod(MessageName="getRangRandom")]
[System.Web.Script.Services.ScriptMethod(UseHttpGet=true)]
[System.Web.Services.WebMethod(EnableSession=true)]
public static int GetRandom(int begin,int end)
{
Random random = new Random(DateTime.Now.Millisecond);
return random.Next(begin,end);
}
[System.Web.Services.WebMethod(EnableSession=true)]用于标识一个服务器方法启用session。
在开发中,可能会遇到这样的问题:服务器端公开了两个方法,其中一个方法是对另一个方法的重载。也就是有这两个方法的名称是相同的,只是方法签名不一样。此时,我们会发现,尽管服务器公开了两个方法,但我们只能使用其中的某一个,当查看自动生成的JS代理时,你会发现,页面中只生成了一个方法的代理。为什么会有这样问题?
asp.net ajax框架在为服务器方法生成代理时,是以JSON字典的方式来存储方法信息的,方法名称为字典的Key,Value为方法的其它信息。当遇到两个方法名相同的方法时(即Key相同),最后一个对相同key的操作会覆盖前面对这个Key的操作,所以当生成代理是,只会有一个方法的代理会被生成。
解决这种问题的方法比较简单,即编写服务器端方法时,在WebMethod的构造器中指定将要被生成到客户端的相应方法名称即可,即
[System.Web.Services.WebMethod]
public static int GetRandom()
{
Random random = new Random(DateTime.Now.Millisecond);
return random.Next();
}
[System.Web.Services.WebMethod(MessageName="getRangRandom")]
public static int GetRandom(int begin,int end)
{
Random random = new Random(DateTime.Now.Millisecond);
return random.Next(begin,end);
}
下面是一个完整的示例:
<%@ Page Language="C#" %>
<%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
Namespace="System.Web.UI" TagPrefix="asp" %>
[System.Web.Services.WebMethod]
public static int GetRandom()
{
Random random = new Random(DateTime.Now.Millisecond);
return random.Next();
}
[System.Web.Services.WebMethod(MessageName="getRangRandom")]
public static int GetRandom(int begin,int end)
{
Random random = new Random(DateTime.Now.Millisecond);
return random.Next(begin,end);
}
改变服务器在客户端对应的方法名称
PageMethods.set_timeout(5000);
PageMethods.set_defaultFailedCallback="OnFailed";
function getRandom(begin,end)
{
if ( arguments.length != 2 )
{
PageMethods.GetRandom(OnSucceed);
}
else
{
PageMethods.getRangRandom(begin,end,OnSucceed);
}
}
function OnSucceed(Message)
{
alert(Message);
}
function OnFailed(ErrorHander)
{
var ErrorMessage = '是否超时'+ErrorHander.get_timedOut()+'/n错误信息为:' + ErrorHander.get_message();
ErrorMessage = ErrorMessage + '/n异常名称为:' + ErrorHander.get_exceptionType() + '/n错误位置为:' + ErrorHander.get_stackTrace();
alert(ErrorMessage);
}
二、改变客户端访问服务器端的方式
默认情况下,在asp.net ajax中客户端访问服务器端时使用的是HTTP POST方式,如果您想使用HTTP GET的方式来访问服务器端,即可以这样做:
[System.Web.Services.WebMethod(MessageName="getRangRandom")]
[System.Web.Script.Services.ScriptMethod(UseHttpGet=true)]
public static int GetRandom(int begin,int end)
{
Random random = new Random(DateTime.Now.Millisecond);
return random.Next(begin,end);
}
这样,当客户端在调用GetRandom(int begin,int end)方法时,就是以HTTP GET的方式来调用。
三、在asp.net ajax中使用session
[System.Web.Services.WebMethod(MessageName="getRangRandom")]
[System.Web.Script.Services.ScriptMethod(UseHttpGet=true)]
[System.Web.Services.WebMethod(EnableSession=true)]
public static int GetRandom(int begin,int end)
{
Random random = new Random(DateTime.Now.Millisecond);
return random.Next(begin,end);
}
[System.Web.Services.WebMethod(EnableSession=true)]用于标识一个服务器方法启用session。
相关文章推荐
- 使用ASP.NET AJAX框架扩展HTML Map控件
- ASP.NET MV框架下使用ajax上传要注意的一个问题
- ASP.NET MVC中使用AJAX传参技巧
- ASP.NET AJAX Control Toolkit: ModalPopupExtender使用技巧(转)
- WebCast《实战ASP.NET AJAX系列课程(2):使用客户端框架创建“纯粹”的Ajax应用程序》相关资源
- 在ASP.NET中使用无框架的Ajax实例
- 分享两个实用的ASP.NET开发技巧——使用WebMethod实现ajax和控件的ClientIDMode属性
- 在ASP.NET中使用无框架的Ajax实例
- ASP.NET ajax.dll ajaxmethod使用技巧总结
- (五)asp.net ajax框架的简单使用
- 在ASP.NET中使用无框架的Ajax实例
- asp.net ajax框架使用-DataTable的使用
- asp.net ajax UpdatePanel使用技巧以及常见问题
- 转载: asp.net ajax UpdatePanel使用技巧以及常见问题
- 使用ASP.NET AJAX 1.0框架PreviewGlitz进行网页动画编程
- asp.net MVC 使用JQuery.Ajax
- asp.net使用AJAX实现无刷新分页
- 使用ASP.NET AJAX访问Script Method时启用客户端缓存
- asp.net ajax toolkit中的CascadingDropDown使用详解(二)
- ASP.NET AJAX(8)__Microsoft AJAX Library中异步通信层的使用