<IMG SRC="images\solarsys.gif" WIDTH=504 HEIGHT=126 BORDER=0 ALT="Solar System" USEMAP="#SystemMap"> <MAP NAME="SystemMap"> <AREA SHAPE="rect" COORDS="0,0,82,126" onmouseover="javascript:GetAreaInfo(event, 'sun');" onmouseout="javascript:HidePopup();"> <AREA SHAPE="circle" COORDS="90,58,3" onmouseover="javascript:GetAreaInfo(event, 'merglobe');" onmouseout="javascript:HidePopup();" > <AREA SHAPE…………(省略) </MAP>在上面代码中,我们添加了一个HTML 元素和一个HTML 元素(注:VS2005工具栏中没有提供现成的控件,只能手工添加)。其中定义了各个星球相应的热点形状及坐标信息。而且,每一个热点都有一个相应的onmouseover和 onmouseout Script函数与之相关联。当鼠标在这些热点上移动时,这两个函数将被激活,相应信息被显示出来。有关这两个函数,我们将在后面详细讨论。
[ScriptService()]
public class LocationService : System.Web.Services.WebService
{}
[WebMethod]
[ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
public string GetAreaInfo(string area)
{
return area;
}
根据权威人士建议,为了起见,我们一般要使用HttpPost(或者HttpGet= false)方式访问Web方法。然后,我们把返回的数据格式配置为JSON格式(默认方式即为JSON方式)。 <asp:ScriptManager ID="ScriptManager1" runat="server"> <services> <asp:servicereference path="~/LocationService.asmx" /> </services> </asp:ScriptManager>在此,我们仅在节点下加入了一个服务参考,但其作用如何呢?
<script src="LocationService.asmx/jsdebug" type="text/javascript"></script>这里的脚本标签引用了一个Script文件LocationService.asmx/jsdebug。其实这是一个Web服务代理类。正是通过此代理类,我们才得以从客户端以异步方式调用服务器端的Web服务。
Type.registerNamespace("MyServices");
接下来,我们定义要创建的客户端类的构造函数:MyServices.Location = function (uiElement, uiBody) {
MyServices.Location.initializeBase(this);
this._uiElement = uiElement;
this._uiBody = uiBody;
this._xAxis = 0;
this._yAxis = 0;
}
一个模板或类的构造函数也只不过是一个普通的JavaScript函数。该构造器共有两个参数:uiElement和uiBody。 MyServices.Location.prototype =
{
get_uiElement: function()
{
return this._uiElement;
},
set_uiElement: function(value)
{
this._uiElement = value;
},
get_uiBody: function()
{
return this._uiBody;
},
set_uiBody: function(value)
{
this._uiBody = value;
},
注意,这里的UI元素属性方法的定义方式非常类似于.NET中各种语言中的定义形式。 ShowPopupinfo: function(event, areaName)
{
MyServices.LocationService.GetAreaInfo(areaName,
Function.createDelegate(this, this.OnCompleted),
this.OnError, //负责进行错误处理的回叫函数
this.OnTimeOut); //负责进行超时处理的回叫函数
this._xAxis = event.clientX;
this._yAxis = event.clientY;
}
上面的代码展示的是非常典型的从客户端调用Web服务的方法: OnCompleted: function(result, userContext, methodName)
{
var uiElement = $get(this.get_uiElement());
var uiBody = $get(this.get_uiBody());
if (uiBody != null)
{
var textNode = uiBody.firstChild;
if (!textNode)
{
textNode = document.createTextNode(result);
uiBody.appendChild(textNode);
}
else
{
textNode.nodeValue = result;
}
if (uiElement != null)
{
uiElement.style.visibility = "visible";
uiElement.style.display = "inline";
uiElement.style.left = this._xAxis + "px";
uiElement.style.top = this._yAxis + "px";
}
}
},
内容相当简单—把从服务器端返回的数据设置为弹出窗口的显示内容并根据情况确保显示此窗口。 MyServices.Location.registerClass("MyServices.Location");
至此,客户端类MyServices.Location已经成功创建。那么,如何使用它呢? var location = null;
function pageLoad(sender, args) {
location = new MyServices.Location("modal", "modalBody");
location.HidePopupInfo();
}
上面的代码简单地创建MyServices.Location类的一个新的实例。然后调用客户端类的成员函数之一来隐藏页面中的弹出窗口。为什么我们在pageLoad函数中创建客户端类的一个实例呢?原因在于,当AJAX环境控制流程到达pageLoad函数时,所有的AJAX客户端和用户定义的JavaScript代码都已经被成功加载。因此,这一时刻我们可以安全地访问任何用户或系统定义的 JavaScript代码。 文档说明:
相关文档
返回首页 | 关于本站 | | 友情链接 | 广告服务 | 意见建议 | 访客留言 | 本站论坛
Copyright© 2001-2006 ProgramBBS.com All Rights Reserved 版权所有©编程论坛
Email: 吉ICP备05009985号
感谢长春订餐网友情支持