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按鈕的屬性中 :οnclick="return btnStreet1_onclick()"
Toolbar 上的按鈕 : ClientAction="SetMapView()"
// SetMapView() 類同於上述js方法
超鏈接 : <a href="#" class="padingClass" οnclick="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 );
// 以上這兩句話非常重要,將結果返回給客戶端
}
能夠總結的也就這麼多,隨着項目的深入,以後再擴展。