在ASP.NET中替换Sys.Services的方法
在ASP.NET中替换Sys.Services的方法使用自定义类替换Sys.Services.ProfileService对象
一般来说,这是最容易想到的办法。我们可以写一个类替换Sys.Services._ProfileService 类(这个类完全通过 prototype扩展,因此对于继承非常友好),甚至完全重写一个类,这个一般就看具体情况了。假设我们已经定义了这么一个类 “Jeffz.Services.ProfileService”,并将其包含在MyProfile.Service.js中,就要开始使用了。那么还要注意些什么呢?
需要注意的就是顺序,我们一般会使用ScriptManager引入该JS,如下:
www.189works.com(天翼空间应用工厂)
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="false"> <Scripts> <asp:ScriptReference Path="MyProfileService.js" /> Scripts> <ProfileService LoadProperties="ZipCode, Address.City" Path="MyProfile.asmx"/> asp:ScriptManager>
我们为ProfileService节点加上了LoadProperties属性,表明需要预加载Profile中的ZipCode和Address这个 Profile Group下的City属性。另外,我们将EnablePartialRendering属性设为了False,避免出现多余的代码。
<script src="/Value-Add-WebSite/WebResource.axd?d=...;t=..." type="text/javascript"></script> <script type="text/javascript"> </script> <script src="MyProfileService.js" type="text/javascript"></script>
第一行引入的是 MicrosoftAjax.js,它之中定义了ASP.NET AJAX中默认的ProfileService,而紧接着就是对于ProfileService的使用:设定其Path以及预加载的 Properties。在引入之后千万不能忘了要将这些信息进行保留。但是这两者之间无法插入任何代码,因此我们可以在 MyProfileService.js里添加如下的代码,以保留这些信息:
var path = Sys.Services.ProfileService.get_path(); if (!path) { path = Sys.Services._ProfileService.WebServicePath; } var properties = Sys.Services.ProfileService.properties; var newnewInstance = new Jeffz.Services.ProfileService(); newInstance.set_path(path); newInstance.properties = properties; Sys.Services.ProfileService = newInstance;
当然,可能代码会根据实际情况略有不同,但是注意JavaScript引入以及执行的顺序,在做任何自定义工作时都是非常重要的。
有人也许会问,既然已经重新定义了自己的实现,为什么还要将其“伪装”成默认的ProfileService呢?因为这种“自定义”其实并不为“官方” 所承认,这么做能够保证了兼容性,保证了第三方的组件也能使用Profile Service,即使它们没有“意识”到没有使用ASP.NET AJAX提供的默认Profile Service。以上介绍在ASP.NET中替换Sys.Services的方法
--------------------编程问答-------------------- 楼主辛苦了,在这个板块里发了这么多帖子
补充:.NET技术 , ASP.NET