要实现ICallbackEventHandler接口并提供RaiseCallbackEvent()方法和
public string GetCallbackResult()方法,只有这样的话才能够实现有调有回,然后在Page_Load中使用
GetCallbackEventReference()架起一座桥梁.参数的意义请参考其他文档.这里以附带的
Common_Callback_CSharp例子加以说明.所做的内容就是实现两种方法定位地图中心:地名和坐标.首先在客户端实现此代码:
<script language="javascript">
//根据地名定位地图中心
function ZoomToLocationClient(val)
{
var message = 'zoomtolocation';
message += ',' + val;
var context = 'Map1';
//把服务端的生成的脚本段sCallBackFunctionInvocation输出
//输出内容:WebForm_DoCallback('__Page',message,processCallbackResult,context,postBackError,true)
<%=sCallBackFunctionInvocation%>
}
//根据坐标定位地图中心
function ZoomToPointClient()
{
//获取输入的x值
var x = document.getElementById('TextBoxX').value;
//获取输入的y值
var y = document.getElementById('TextBoxY').value;
//生成请求字符串
var message = 'zoomtopoint';
message += ',' + x + ',' + y;
var context = 'Map1';
//把服务端的生成的脚本段sCallBackFunctionInvocation输出
//输出内容:WebForm_DoCallback('__Page',message,processCallbackResult,context,postBackError,true)
<%=sCallBackFunctionInvocation%>
}
</script>
相应的客户端代码为:
//根据坐标定位地图中心
public void ZoomToPointServer(string ea)
{
char[] parser_char = { ',' };
string[] messages = ea.Split(parser_char);
double map_width_eight = Map1.Extent.Width/8;
double x_center = Double.Parse(messages[1]);
double y_center = Double.Parse(messages[2]);
// Map control changes extent, recognizes that a new Web ADF tile cache needs to generated by the client
ESRI.ArcGIS.ADF.Web.Geometry.Envelope env = new ESRI.ArcGIS.ADF.Web.Geometry.Envelope(x_center - map_width_eight, y_center - map_width_eight, x_center + map_width_eight, y_center + map_width_eight);
Map1.Extent = env;
// The callback string generated by the Map control is returned to the client.
mapstring = Map1.CallbackResults.ToString();
}
//根据名称定位地图中心
public void ZoomToLocationServer(string ea)
{
//请求字符串分割处理
char[] parser_char = { ',' };
string[] messages = ea.Split(parser_char);
string location = messages[1];
double minx = 0;
double miny = 0;
double maxx = 0;
double maxy = 0;
bool validlocation = false;
//根据地点名称不同定位到不同的坐标
//这里为了方便直接给min和max赋值,在实际的开发中可以更加地点名称进行坐标的查询然后更加查询的坐标进行定位
switch (location)
{
case "Asia":
minx = -125.0;
miny = 30.0;
maxx = -110.0;
maxy = 45.0;
validlocation = true;
break;
case "Africa":
minx = -80.0;
miny = 40.0;
maxx = -70.0;
maxy = 45.0;
validlocation = true;
break;
default:
break;
}
if (validlocation)
{
ESRI.ArcGIS.ADF.Web.Geometry.Envelope new_extent = new ESRI.ArcGIS.ADF.Web.Geometry.Envelope(minx, miny, maxx, maxy);
//设置当前地图的Extent
Map1.Extent = new_extent;
//把CallbackResults返回给客户端,让客户端更新地图显示
mapstring = Map1.CallbackResults.ToString();
} else {
mapstring = "";
}
}
然后客户端通过
<asp:MenuItem Text="Zoom To" Value="Zoom To">
<asp:MenuItem Text="Asia" Value="Asia" NavigateUrl="javascript: ZoomToLocationClient('Asia');"></asp:MenuItem>
<asp:MenuItem Text="Africa" Value="Africa" NavigateUrl="javascript: ZoomToLocationClient('Africa');"></asp:MenuItem>
</asp:MenuItem>向服务器注册请求.
服务器通过在Page_Load中实现下面代码响应请求并向提供RaiseCallbackEvent()参数:
sCallBackFunctionInvocation = Page.ClientScript.GetCallbackEventReference(this, "message", "processCallbackResult", "context", "postBackError", true);
RaiseCallbackEvent()获得参数后继续交给GetCallbackEventReference()由其处理最终的结果,最后由GetCallbackResult()回调给客户端处理的结果.
RaiseCallbackEvent()代码:
public void RaiseCallbackEvent(string eventArgs)
{
//根据坐标定位地图中心
if (eventArgs.Contains("zoomtopoint"))
{
ZoomToPointServer(eventArgs);
}
//根据名称定位地图中心
else if (eventArgs.Contains("zoomtolocation"))
{
ZoomToLocationServer(eventArgs);
}
}
GetCallbackResult()代码:
public string GetCallbackResult()
{
return mapstring;
}:
<script language="javascript">
//根据地名定位地图中心
function ZoomToLocationClient(val)
{
var message = 'zoomtolocation';
message += ',' + val;
var context = 'Map1';
//把服务端的生成的脚本段sCallBackFunctionInvocation输出
//输出内容:WebForm_DoCallback('__Page',message,processCallbackResult,context,postBackError,true)
<%=sCallBackFunctionInvocation%>
}
//根据坐标定位地图中心
function ZoomToPointClient()
{
//获取输入的x值
var x = document.getElementById('TextBoxX').value;
//获取输入的y值
var y = document.getElementById('TextBoxY').value;
//生成请求字符串
var message = 'zoomtopoint';
message += ',' + x + ',' + y;
var context = 'Map1';
//把服务端的生成的脚本段sCallBackFunctionInvocation输出
//输出内容:WebForm_DoCallback('__Page',message,processCallbackResult,context,postBackError,true)
<%=sCallBackFunctionInvocation%>
}
</script>
相应的客户端代码为:
//根据坐标定位地图中心
public void ZoomToPointServer(string ea)
{
char[] parser_char = { ',' };
string[] messages = ea.Split(parser_char);
double map_width_eight = Map1.Extent.Width/8;
double x_center = Double.Parse(messages[1]);
double y_center = Double.Parse(messages[2]);
// Map control changes extent, recognizes that a new Web ADF tile cache needs to generated by the client
ESRI.ArcGIS.ADF.Web.Geometry.Envelope env = new ESRI.ArcGIS.ADF.Web.Geometry.Envelope(x_center - map_width_eight, y_center - map_width_eight, x_center + map_width_eight, y_center + map_width_eight);
Map1.Extent = env;
// The callback string generated by the Map control is returned to the client.
mapstring = Map1.CallbackResults.ToString();
}
//根据名称定位地图中心
public void ZoomToLocationServer(string ea)
{
//请求字符串分割处理
char[] parser_char = { ',' };
string[] messages = ea.Split(parser_char);
string location = messages[1];
double minx = 0;
double miny = 0;
double maxx = 0;
double maxy = 0;
bool validlocation = false;
//根据地点名称不同定位到不同的坐标
//这里为了方便直接给min和max赋值,在实际的开发中可以更加地点名称进行坐标的查询然后更加查询的坐标进行定位
switch (location)
{
case "Asia":
minx = -125.0;
miny = 30.0;
maxx = -110.0;
maxy = 45.0;
validlocation = true;
break;
case "Africa":
minx = -80.0;
miny = 40.0;
maxx = -70.0;
maxy = 45.0;
validlocation = true;
break;
default:
break;
}
if (validlocation)
{
ESRI.ArcGIS.ADF.Web.Geometry.Envelope new_extent = new ESRI.ArcGIS.ADF.Web.Geometry.Envelope(minx, miny, maxx, maxy);
//设置当前地图的Extent
Map1.Extent = new_extent;
//把CallbackResults返回给客户端,让客户端更新地图显示
mapstring = Map1.CallbackResults.ToString();
} else {
mapstring = "";
}
}
然后客户端通过
<asp:MenuItem Text="Zoom To" Value="Zoom To">
<asp:MenuItem Text="Asia" Value="Asia" NavigateUrl="javascript: ZoomToLocationClient('Asia');"></asp:MenuItem>
<asp:MenuItem Text="Africa" Value="Africa" NavigateUrl="javascript: ZoomToLocationClient('Africa');"></asp:MenuItem>
</asp:MenuItem>向服务器注册请求.
服务器通过在Page_Load中实现下面代码响应请求并向提供RaiseCallbackEvent()参数:
sCallBackFunctionInvocation = Page.ClientScript.GetCallbackEventReference(this, "message", "processCallbackResult", "context", "postBackError", true);
RaiseCallbackEvent()获得参数后继续交给GetCallbackEventReference()由其处理最终的结果,最后由GetCallbackResult()回调给客户端处理的结果.
RaiseCallbackEvent()代码:
public void RaiseCallbackEvent(string eventArgs)
{
//根据坐标定位地图中心
if (eventArgs.Contains("zoomtopoint"))
{
ZoomToPointServer(eventArgs);
}
//根据名称定位地图中心
else if (eventArgs.Contains("zoomtolocation"))
{
ZoomToLocationServer(eventArgs);
}
}
GetCallbackResult()代码:
public string GetCallbackResult()
{
return mapstring;
}: