作者:水的右边(GIS特邀嘉宾)
目的:
1.arcgis server9.2 ADF的无刷新机制。
准备:
1.(一、一)的工程,具体见前篇。
开始:
1. 先把上篇里漏下的ScaleBar(比例尺)和Magnifier(放大镜)功能补上,从工具栏拖一个ScaleBar和Magnifier控件到页面上ID分别为ScaleBar1和Magnifier1。
2.设置ScaleBar1,首先给esri:ScaleBar加上Style=" left: 278px; position: absolute; top: 485px; "这样做的目的就是可以使得ScaleBar1浮在地图上面,把页面切换到设计视图,选择ScaleBar1把它位置拖动到地图的右下角的位置,设置Map属性为Map1,设置BarUnits(单位)属性为Kilometers(千米), BarUnits有Miles(英里)、Kilometers(千米)、Feet(英尺)、Meters(米)四种选择,找了一下汉化单位的方法好像没有找到了,只能这样了。
3.设置Magnifier1,首先给esri:Magnifier加上style="position: absolute; left: 179px; top: 122px;"然后在设计视图中拖动到合适的位置,设置Map属性为Map1,MagnifierMapResource属性为MapResourceManager1,MagnifierMapResource属性为MapResourceItem0,MagnificationFactor属性设置默认放大倍数。
4.这样就完成了ScaleBar(比例尺)和Magnifier(放大镜)功能,可以调试运行一下查看效果。
5.开始功能代码,Default.aspx页面切换到代码视图,按照页生命周期顺序来说明代码。
6.首先添加ESRI.ArcGIS.ADF.Web.UI.WebControls的引用,输入页面的Page_PreInit方法,代码如下:
代码:
protected void Page_PreInit(object sender, EventArgs e)
{
//找到页面中的OverviewMap_Panel控件
FloatingPanel ovPanel = Page.FindControl("OverviewMap_Panel") as FloatingPanel;
if (ovPanel !=null)
{
OverviewMap ov = ovPanel.FindControl("OverviewMap1") as OverviewMap;
//如果OverviewMap1不为空OverviewMap_Panel的状态为展开状态
if (ov !=null)
{
ov.Enabled = ovPanel.Expanded;
}
}
}
7.Page_Load事件,首先去掉了一些比较复杂的代码从简单到复杂的分析,代码和说明如下:
代码:
public string m_newLoad ="false";
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsCallback &&!Page.IsPostBack)
{
//Map1控件的MapResourceManager检查
if (Map1.MapResourceManager ==null|| Map1.MapResourceManager.Length ==0)
{
//调用页面错误方法
callErrorPage("No MapResourceManager defined for the map.", null);
}
//MapResourceManager1控件的ResourceItems检查
if (MapResourceManager1.ResourceItems.Count ==0|| MapResourceManager1.ResourceItems[0] ==null)
{
//调用页面错误方法
callErrorPage("The MapResourceManager does not have a valid ResouceItem Definition.", null);
}
//
m_newLoad ="true";
}
//获取OverviewMap_Panel控件
FloatingPanel ovPanel = Page.FindControl("OverviewMap_Panel") as FloatingPanel;
if (ovPanel !=null)
{
//设置OverviewMap_Pane的关闭事件
ovPanel.PanelCollapsed +=new ESRI.ArcGIS.ADF.Web.UI.WebControls.FloatingPanelCollapseEventHandler(OverviewMap_Panel_PanelCollapsed);
//设置OverviewMap_Pane的展开事件
ovPanel.PanelExpanded +=new ESRI.ArcGIS.ADF.Web.UI.WebControls.FloatingPanelExpandEventHandler(OverviewMap_Panel_PanelExpanded);
}
}
//OverviewMap的展开事件
private void OverviewMap_Panel_PanelExpanded(object sender, EventArgs args)
{
FloatingPanel ovPanel = Page.FindControl("OverviewMap_Panel") as FloatingPanel;
if (ovPanel !=null)
{
OverviewMap ov = ovPanel.FindControl("OverviewMap1") as OverviewMap;
if (ov !=null)
{
//激活OverviewMap1鹰眼
ov.Enabled =true;
//刷新鹰眼
ov.Refresh();
//进行回调登记
ovPanel.CallbackResults.CopyFrom(ov.CallbackResults);
}
}
}
//OverviewMap的关闭事件
private void OverviewMap_Panel_PanelCollapsed(object sender, EventArgs args)
{
FloatingPanel ovPanel = Page.FindControl("OverviewMap_Panel") as FloatingPanel;
if (ovPanel !=null)
{
OverviewMap ov = ovPanel.FindControl("OverviewMap1") as OverviewMap;
if (ov !=null)
{
//不激活OverviewMap1鹰眼
ov.Enabled =false;
}
}
}
//发生错误是跳到ErrorPage.aspx页面
private void callErrorPage(string errorMessage, Exception exception)
{
//存储错误信息到Session
Session["ErrorMessage"] = errorMessage;
Session["Error"] = exception;
Page.Response.Redirect("ErrorPage.aspx", true);
}
8.接下来页面要实现 ICallbackEventHandler这个接口,关于 ICallbackEventHandler的资料网上很多了,通过实现 ICallbackEventHandler接口实现了NET中页面的无回调刷新。 实现 ICallbackEventHandler接口就必须实现string ICallbackEventHandler.GetCallbackResult()和void ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)这2个方法具体代码和说明如下:
代码:
public partial class _Default : System.Web.UI.Page, ICallbackEventHandler
{
//ICallbackEventHandler 成员
#region ICallbackEventHandler 成员
//负责把结果回传给客户端
private string ICallbackEventHandler.GetCallbackResult()
{
throw new Exception("The method or operation is not implemented.");
}
//负责从客户端javascript传来的参数
private void ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)
{
throw new Exception("The method or operation is not implemented.");
}
#endregion
}
9.用ICallbackEventHandler的方式实现页面关闭和页面版权显示2个功能,首先添加m_closeOutCallback和m_copyrightCallback两个全局的字符串变量,这两字符串是生成客户端的javascript用的。然后在Page_Load事件添加生产这两字符内容的代码,具体代码如下
代码:
//关闭javascript段
public string m_closeOutCallback = "";
//版权javascript段
public string m_copyrightCallback = "";
protected void Page_Load(object sender, EventArgs e)
{
//通过GetCallbackEventReference生成客户端调用的javascript方法段
//GetCallbackEventReference (Control control,string argument,string clientCallback,string context)
//参数:
//control 处理客户端回调的服务器 Control。该控件必须实现 ICallbackEventHandler 接口并提供 RaiseCallbackEvent 方法。
//argument 从客户端脚本传递一个参数到服务器端的RaiseCallbackEvent 方法。
//clientCallback 一个客户端事件处理程序的名称,该处理程序接收服务器端事件返回的结果。
//context 启动回调之前在客户端的客户端脚本信息。脚本的结果传回给客户端事件处理程序。
m_closeOutCallback = Page.ClientScript.GetCallbackEventReference(Page, "argument", "CloseOutResponse", "context", true);
m_copyrightCallback = Page.ClientScript.GetCallbackEventReference(Page, "argument", "processCallbackResult", "context", true);
}
10.把上面的m_closeOutCallback和m_copyrightCallback输出到页面,切换到Default.aspx的html代码页面在页面的结尾处输入如下代码:
代码:
.....
</form>
<script language="javascript" type="text/javascript">
newLoad = <%=m_newLoad %>;
webMapAppCloseCallback = "<%=m_closeOutCallback %>";
webMapAppCopyrightCallback = "<%=m_copyrightCallback %>";
startUp();
</script>
</body>
</html>
11.新建JavaScript目录,然后在目录中新建叫WebMapApp.js的js文件,同时在Default.aspx页面用添加对WebMapApp.js文件的引用:<script language="javascript" type="text/javascript" src="javascript/WebMapApp.js"></script> 。WebMapApp.js代码如下:
代码:
//初始化方法
function startUp()
{
//暂空
}
//关闭页面方法
function CloseOut() {
//向服务器端传递的参数
var argument = "ControlID=Map1&ControlType=Map&EventArg=CloseOutApplication";
var context = map.controlName;
//用eval执行webMapAppCloseCallback字符串,webMapAppCloseCallback = "WebForm_DoCallback('__Page',argument,CloseOutResponse,context,null,true)";
eval(webMapAppCloseCallback);
}
//在CloseOut()方法后接收服务器端事件返回的结果对页面进行响应
function CloseOutResponse(response, context) {
window.close();
document.location = response;
}
function webMapAppGetCopyrightText() {
//向服务器端传递的参数
var argument = "ControlID=Map1&ControlType=Map&EventArg=GetCopyrightText";
var context = map.controlName;
//用eval执行webMapAppCopyrightCallback字符串,webMapAppCopyrightCallback = "WebForm_DoCallback('__Page',argument,processCallbackResult,context,null,true)";
eval(webMapAppCopyrightCallback);
//显示CopyrightText_Panel,arcgisServer的方法
showFloatingPanel('CopyrightText_Panel');
}
12.上面的脚本文件中主要实现了CloseOut()和webMapAppGetCopyrightText()两客户端方法,他们是通过执行有服务器端GetCallbackEventReference生成客户端调用的javascript方法段实现无刷新的调用服务器端的方法。
13.接下来在服务器端编写响应上面这2个客户端方法的代码,这2个客户端方法都是通过WebForm_DoCallback与服务器端进行交互,所以需要在服务器端的ICallbackEventHandler.GetCallbackResult()和ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)方法中进行处理,具体代码和说明如下:
代码:
// ICallbackEventHandler 成员
#region ICallbackEventHandler 成员
private string _callbackArg;
//负责把结果回传给客户端
private string ICallbackEventHandler.GetCallbackResult()
{
return RaiseCallbackEvent(_callbackArg);
}
//负责从客户端javascript传来的参数
private void ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)
{
_callbackArg = eventArgument;
}
//对客户端的请求进行处理
public virtual string RaiseCallbackEvent(string responseString)
{
//对请求字符串进行分割以键值的形式放到NameValueCollection m_queryString中,方便接下来的使用
Array keyValuePairs = responseString.Split("&".ToCharArray());
NameValueCollection m_queryString = new NameValueCollection();
string[] keyValue;
string response = "";
if (keyValuePairs.Length > 0)
{
for (int i = 0; i < keyValuePairs.Length; i++)
{
keyValue = keyValuePairs.GetValue(i).ToString().Split("=".ToCharArray());
m_queryString.Add(keyValue[0], keyValue[1]);
}
}
else
{
keyValue = responseString.Split("=".ToCharArray());
if (keyValue.Length > 0)
{
m_queryString.Add(keyValue[0], keyValue[1]);
}
}
//请求字符串样例:ControlID=Map1&ControlType=Map&EventArg=CloseOutApplication,这样就可以很容易理解了
string controlType = m_queryString["ControlType"];
string eventArg = m_queryString["EventArg"];
if (controlType == null)
{
controlType = "Map";
}
//根据controlType的不同对请求做不同的处理
switch (controlType)
{
case "Map":
if (eventArg == "CloseOutApplication")//关闭页面请求-CloseOut()
{
//ServerContext对象,需要 ESRI.ArcGIS.Server;
IServerContext context;
for (int i = 0; i < Session.Count; i++)
{
//清除session
context = Session[i] as IServerContext;
if (context != null)
{
context.RemoveAll();
context.ReleaseContext();
}
}
Session.RemoveAll();
//从webconfig中获取关闭后的页面地址
response = ConfigurationManager.AppSettings["CloseOutUrl"];
if (response == null || response.Length == 0)
{
response = "ApplicationClosed.aspx";
}
}
else if (eventArg == "GetCopyrightText")//显示版权-webMapAppGetCopyrightText()
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
//webMapAppGetCopyrightText()请求服务器后返回结果由processCallbackResult进行客户端处理
//关于processCallbackResult方法的参数格式 控件类型:::控件ID:::内容类型:::内容,如div:::mydiv:::content:::你好,GIS!
sb.AppendFormat("///:::{0}:::innercontent:::", "CopyrightTextContents");
int sbLength = sb.Length;
//获取版权内容
sb.Append(GetCopyrightText());
if (sb.Length == sbLength)
{
//没有获取,显示为没有版权
sb.Append("No Copyright information available.");
}
response = sb.ToString();
}
break;
default:
break;
}
//输出给客户端的内容
return response;
}
#endregion
private string GetCopyrightText()
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
string key = "";
string value = "";
string dataframeValue = "";
int layerId;
foreach (IMapFunctionality mapFunc in Map1.GetFunctionalities())
{
//
if (mapFunc.Supports("GetCopyrightText"))
{
Dictionary<string, string> crDictionary = mapFunc.GetCopyrightText();
System.Text.StringBuilder sb2 = new System.Text.StringBuilder();
string[] layerIds = null;
string[] layerNames = null;
mapFunc.GetLayers(out layerIds, out layerNames);
foreach (KeyValuePair<string, string> kvPair in crDictionary)
{
key = kvPair.Key;
value = kvPair.Value;
if (value != null && value.Length > 0)
{
if (key != null && key.Length > 0)
{
layerId = Convert.ToInt32(key);
sb2.Append(layerNames[layerId] + ": ");
sb2.Append(value + "<br/>");
}
else
dataframeValue = value;
}
}
if (sb2.Length > 0) sb.AppendFormat("<div style='font-weight: bold'>{0}:</div><div style='padding: 0px 5px 5px 5px;font-weight: normal;'>{2}<br/>{1}</div>", mapFunc.Resource.Name, sb2.ToString(), dataframeValue);
sb2.Length = 0;
dataframeValue = "";
}
}
return sb.ToString();
}
14.现在在页面上添加一个FloatingPanel控件id为CopyrightText_Panel,在<esri:FloatingPanel标签中添加 Style="left: 540px; position: absolute; top: 145px",Visible属性为False,在这个FloatingPanel控件添加一个div id为CopyrightTextContents.代码如下:
代码:
<esri:FloatingPanel ID="CopyrightText_Panel" runat="server" BackColor="White" BorderColor="Gray"
BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana" Font-Size="8pt" ForeColor="Black"
Height="200px" Style="left: 491px; position: absolute; top: 387px"
Title="Copyright" TitleBarColor="WhiteSmoke" TitleBarHeight="20px"
TitleBarSeparatorLine="False" Transparency="35" Width="250px" Visible="False" Docked="False">
<div id="CopyrightTextContents" style="width: 100%" >Copyright Information</div>
</esri:FloatingPanel>
15.新建ApplicationClosed.aspx页面,等关闭地图页面后显示的页面。
16.最好在页面上添加2个btton来调用CloseOut()和webMapAppGetCopyrightText()方法,具体代码如下:
代码:
<input id="Button1" type="button" value="关闭地图" onclick="CloseOut()" />
<input id="Button2" type="button" value="显示版权"onclick="webMapAppGetCopyrightText()"/>
17.最后调试运行看效果,剩下的部分下篇在写。
![](https://i-blog.csdnimg.cn/blog_migrate/555b604665d6171b1d798ccc4bb14d7e.jpeg)
17.最后调试运行看效果,剩下的部分下篇在写。