ArcGIS Server 9.3 中AJAX支持两种实现方式:Callback机制和PartialPostback 机制。
Callback机制
1、Callback机制的实现过程
(1)在客户端的控件一般是html控件、Toolbar上的按钮或者一些超链接(asp.net的控件不行)。在客户端的实现代码一般如下:
<script language="javascript" type="text/javascript">
function btnStreet1_onclick() {
//获取控件的值
var v=document.getElementById("selStreet").value;
//生成请求字符串
var message="ControlID=Map1&ControlType=Map&Type=queryS&EventArg=" + v;
var context = "Map";
// sCallBack 由服务端的Page_load事件中生成的,用于请求服务端的js代码
<%=sCallBack%>
}
</script>
然后在控件的click事件或者其他对应的方法中调用上述js方法。
例如:在html按钮的属性中:onclick="return btnStreet1_onclick()"
Toolbar上的按钮: ClientAction="SetMapView()"
// SetMapView()类同于上述js方法
超链接:<a href="#" class="padingClass" onclick="LocByInterestPoint('lctInterestPoint.aspx');">按兴趣点定位</a>
//LocByInterestPoint('lctInterestPoint.aspx') 类同于上述js方法
(2)在服务器端(后台)的代码一般如下:
//注意首先要继承ICallbackEventHandler接口
public partial class _Default :System.Web.UI.Page,ICallbackEventHandler
{
public string sCallBack = string.Empty;
protected void Page_Load(object sender, EventArgs e)
{
sCallBack = Page.ClientScript.GetCallbackEventReference(this, "message", "processCallbackResult", "context", "postBackError", true);
}
}
然后就是重载ICallbackEventHandler接口的两个成员函数了,
#region ICallbackEventHandler 成员
//负责把结果回传给客户端
public string GetCallbackResult()
{
return smapstring;
}
//负责接收客户端javascript传来的参数,并对客户端的请求进行处理
public void RaiseCallbackEvent(string eventArgument)
{
try
{
if (eventArgument.Contains("SetMapView"))
ZoomToZBExtent();//自定义显示范围的实现函数
else if(eventArgument.Contains("DMLocation"))
LocationByIntePoint();//按兴趣点定位的实现函数
}
catch (Exception Err)
{
string s = Err.Message;
}
}
#endregion
private void ZoomToZBExtent()
{
if (Session["curMapExtentStr"] == null) return;
string sZBStr = Session["curMapExtentStr"].ToString();
if (sZBStr.Trim() == string.Empty) return;
string[] sZB = sZBStr.Split('/');
double dXMin = double.Parse(sZB[1]);
double dYMin = double.Parse(sZB[0]);
double dXMax = double.Parse(sZB[3]);
double dYMax = double.Parse(sZB[2]);
ESRI.ArcGIS.ADF.Web.Geometry.Envelope pEnve = new ESRI.ArcGIS.ADF.Web.Geometry.Envelope(dXMin, dYMin, dXMax, dYMax);
Map1.Extent = pEnve;
Map1.CallbackResults.CopyFrom(Toolbar1.CallbackResults);
smapstring = Map1.CallbackResults.ToString();
}
另外RaiseCallbackEvent的其他实现方式有:
public string RaiseCallbackEvent(string eventArgument)
{
NameValueCollection keyValColl = CallbackUtility.ParseStringIntoNameValueCollection(eventArgument);
if (keyValColl["EventArg"].ToString() == "MapIdentify")
{
…………. //功能实现代码
return Map1.CallbackResults.ToString();
} }
else if (keyValColl["Type"].ToString() == "clear")
{
…………. //功能实现代码
return Map1.CallbackResults.ToString();
}
}
2、一些重要的代码
(1)将其他控件在服务器端处理的结果返回客户端
Toc1.Refresh();
Map1.CallbackResults.CopyFrom(Toc1.CallbackResults);
(2)添加CallbackResult
String javascriptString = "alert('Hello');";
CallbackResult customCallbackResult=
CallbackResult.CreateJavaScript(javascriptString);
Map1.CallbackResults.Add(customCallbackResult);
或者
string value = key.Value.ToString();
CallbackResult callbackResult = new CallbackResult("div",
"Label" + key.Key.ToString(), "innercontent", value);
Map1.CallbackResults.Add(callbackResult);
(3)生成CallbackResult
无参数:CallbackResult callbackResult=new CallbackResult();
三个参数:
CallbackResult callbackResult=new CallbackResult(Control control,string eventarg,params object[]parameters);
例如:
object[] oa = new object[1];
oa[0] = "MyMapTip.style.display='none';";
CallbackResult rst = new CallbackResult(pMap.Page, "javascript", oa);
pMap.CallbackResults.Add(rst);
四个参数:
CallbackResult callbackResult=new CallbackResult(string controlType,string controlID,string eventarg,params object[]parameters);
例如:
string value = key.Value.ToString();
CallbackResult callbackResult = new CallbackResult("div",
"Label" + key.Key.ToString(), "innercontent", value);
Map1.CallbackResults.Add(callbackResult);
其他的例子;
1. 刷新ADF自身的控件TOC:
ESRI.ArcGIS.ADF.Web.UI.WebControls.Toc toc1 = (ESRI.ArcGIS.ADF.Web.UI.WebControls.Toc)map.Page.FindControl("Toc1");
//toc变化代码
toc1.Refresh();
string myTocString= toc1.CallbackResults.ToString();
CallbackResult myTocCallBackString = new CallbackResult("TOC", "Toc1", "content", myTocString);
map.CallbackResults.Add(myTocCallBackString);
2. 刷新asp.net控件
ESRI.ArcGIS.ADF.Web.UI.WebControls.Toc
toc1 = (ESRI.ArcGIS.ADF.Web.UI.WebControls.Toc)map.Page.FindControl("Toc1");
toc1.Nodes.Clear();
toc1.Refresh();
string aaa = toc1.CallbackResults.ToString();
CallbackResult tocstring = new CallbackResult("TOC", "Toc1", "content", aaa);
map.CallbackResults.Add(tocstring);
PartialPostback方法
实现方法一:
要实现PartialPostback,首先要在页面上添加一个ScriptManager控件(页面中的第一个控件)
(1)在客户端的控件一般是asp.net控件(html服务器控件也行)。客户端一般不需要写js代码。
(2)在服务器端(后台)的实现过程如下:
首先将控件注册为异步postback 控件:
protected void Page_Load(object sender, System.EventArgs eventArgs)
{
ScriptManager1.RegisterAsyncPostBackControl(Button1);
}
//下面这段也是进行一些注册和初始化的工作,也是必要的
protected void Page_PreRender(object sender, System.EventArgs eventArgs)
{
string scriptKeyCustom = "customDataItemScript";
if (!this.Page.ClientScript.IsClientScriptBlockRegistered(GetType(), scriptKeyCustom) && !ScriptManager1.IsInAsyncPostBack)
{
string scriptBlock = @" function onLoadFunction(){{
Sys.WebForms.PageRequestManager.getInstance().add_pageLoading(AsyncResponseHandler);
}}
function AsyncResponseHandler(sender, args) {{
var dataItems = args.get_dataItems();
if (dataItems['{0}'] != null)
ESRI.ADF.System.processCallbackResult(dataItems['{0}']);
}}
Sys.Application.add_init(onLoadFunction);";
scriptBlock = string.Format(scriptBlock, Page.ClientID);
this.Page.ClientScript.RegisterStartupScript(GetType(), scriptKeyCustom, scriptBlock, true);
}
}
然后就是具体功能的实现代码了,如下:
protected void Button1_Click(object sender, System.EventArgs eventArgs)
{
double xCenter = double.Parse(TextBoxX.Text);
double yCenter = double.Parse(TextBoxY.Text);
double adfMapWidthEighth = Map1.Extent.Width / 8;
ESRI.ArcGIS.ADF.Web.Geometry.Envelope adfNewExtentEnvelope =
new ESRI.ArcGIS.ADF.Web.Geometry.Envelope(xCenter - adfMapWidthEighth, yCenter - adfMapWidthEighth, xCenter + adfMapWidthEighth, yCenter + adfMapWidthEighth);
Map1.Extent = adfNewExtentEnvelope;
ScriptManager1.RegisterDataItem(Page,Map1.CallbackResults.ToString(), false);
//这句话至观重要,并需的。将结果返回客户端
}
实现方法二:
除了在页面添加ScriptManager控件外,还要添加UpdatePanel 控件。
1)在客户端的控件一般是asp.net控件(html服务器控件也行)。客户端一般不需要写js代码。
(2)在服务器端(后台)的实现过程如下:
首先将控件注册为异步postback 控件:
protected void Page_Load(object sender, System.EventArgs eventArgs)
{
ScriptManager1.RegisterAsyncPostBackControl(Button1);
}
然后就是直接写功能实现函数,如下:
protected void Button1_Click(object sender, System.EventArgs eventArgs)
{
double xCenter = double.Parse(TextBoxX.Text);
double yCenter = double.Parse(TextBoxY.Text);
double adfMapWidthEighth = Map1.Extent.Width / 8;
ESRI.ArcGIS.ADF.Web.Geometry.Envelope adfNewExtentEnvelope =
new ESRI.ArcGIS.ADF.Web.Geometry.Envelope(xCenter - adfMapWidthEighth,
yCenter - adfMapWidthEighth, xCenter + adfMapWidthEighth, yCenter + adfMapWidthEighth);
Map1.Extent = adfNewExtentEnvelope;
string jsProcessCallbackResult = string.Format("ESRI.ADF.System.processCallbackResult('{0}');",
Map1.CallbackResults.ToString().Replace("//", "////"));
System.Web.UI.ScriptManager.RegisterClientScriptBlock(Page, sender.GetType(), "changeextent", jsProcessCallbackResult, true);
//以上这两句话非常重要,将结果返回给客户端
}
能够总结的也就这么多,随着项目的深入,以后再扩展。