目的:
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方法,代码如下:

Code
1
protected void Page_PreInit(object sender, EventArgs e)
2

{
3
//找到页面中的OverviewMap_Panel控件
4
FloatingPanel ovPanel = Page.FindControl("OverviewMap_Panel") as FloatingPanel;
5
if (ovPanel != null)
6

{
7
OverviewMap ov = ovPanel.FindControl("OverviewMap1") as OverviewMap;
8
//如果OverviewMap1不为空OverviewMap_Panel的状态为展开状态
9
if (ov != null)
10

{
11
ov.Enabled = ovPanel.Expanded;
12
}
13
}
14
}
7.Page_Load事件,首先去掉了一些比较复杂的代码从简单到复杂的分析,代码和说明如下:

Code
1
//载入标识
2
public string m_newLoad = "false";
3
4
protected void Page_Load(object sender, EventArgs e)
5
{
6
if (!Page.IsCallback && !Page.IsPostBack)
7
{
8
//Map1控件的MapResourceManager检查
9
if (Map1.MapResourceManager == null || Map1.MapResourceManager.Length == 0)
10
{
11
//调用页面错误方法
12
callErrorPage("No MapResourceManager defined for the map.", null);
13
}
14
//MapResourceManager1控件的ResourceItems检查
15
if (MapResourceManager1.ResourceItems.Count == 0 || MapResourceManager1.ResourceItems[0] == null)
16
{
17
//调用页面错误方法
18
callErrorPage("The MapResourceManager does not have a valid ResouceItem Definition.", null);
19
}
20
//
21
m_newLoad = "true";
22
}
23
24
//获取OverviewMap_Panel控件
25
FloatingPanel ovPanel = Page.FindControl("OverviewMap_Panel") as FloatingPanel;
26
if (ovPanel != null)
27
{
28
//设置OverviewMap_Pane的关闭事件
29
ovPanel.PanelCollapsed += new ESRI.ArcGIS.ADF.Web.UI.WebControls.FloatingPanelCollapseEventHandler(OverviewMap_Panel_PanelCollapsed);
30
//设置OverviewMap_Pane的展开事件
31
ovPanel.PanelExpanded += new ESRI.ArcGIS.ADF.Web.UI.WebControls.FloatingPanelExpandEventHandler(OverviewMap_Panel_PanelExpanded);
32
}
33
}
34
35
//OverviewMap的展开事件
36
void OverviewMap_Panel_PanelExpanded(object sender, EventArgs args)
37
{
38
FloatingPanel ovPanel = Page.FindControl("OverviewMap_Panel") as FloatingPanel;
39
if (ovPanel != null)
40
{
41
OverviewMap ov = ovPanel.FindControl("OverviewMap1") as OverviewMap;
42
if (ov != null)
43
{
44
//激活OverviewMap1鹰眼
45
ov.Enabled = true;
46
//刷新鹰眼
47
ov.Refresh();
48
49
//进行回调登记
50
ovPanel.CallbackResults.CopyFrom(ov.CallbackResults);
51
}
52
}
53
}
54
55
//OverviewMap的关闭事件
56
void OverviewMap_Panel_PanelCollapsed(object sender, EventArgs args)
57
{
58
FloatingPanel ovPanel = Page.FindControl("OverviewMap_Panel") as FloatingPanel;
59
if (ovPanel != null)
60
{
61
OverviewMap ov = ovPanel.FindControl("OverviewMap1") as OverviewMap;
62
if (ov != null)
63
{
64
//不激活OverviewMap1鹰眼
65
ov.Enabled = false;
66
}
67
}
68
}
69
70
//发生错误是跳到ErrorPage.aspx页面
71
private void callErrorPage(string errorMessage, Exception exception)
72
{
73
//存储错误信息到Session
74
Session["ErrorMessage"] = errorMessage;
75
Session["Error"] = exception;
76
77
Page.Response.Redirect("ErrorPage.aspx", true);
78
}
8.接下来页面要实现 ICallbackEventHandler这个接口,关于 ICallbackEventHandler的资料网上很多了,通过实现 ICallbackEventHandler接口实现了NET中页面的无回调刷新。
实现 ICallbackEventHandler接口就必须实现string ICallbackEventHandler.GetCallbackResult()和void ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)这2个方法具体代码和说明如下:

Code
1
public partial class _Default : System.Web.UI.Page, ICallbackEventHandler
2

.
3
4
ICallbackEventHandler 成员#region ICallbackEventHandler 成员
5
6
//负责把结果回传给客户端
7
string ICallbackEventHandler.GetCallbackResult()
8

{
9
throw new Exception("The method or operation is not implemented.");
10
}
11
12
//负责从客户端javascript传来的参数
13
void ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)
14

{
15
throw new Exception("The method or operation is not implemented.");
16
}
17
18
#endregion
9.用ICallbackEventHandler的方式实现页面关闭和页面版权显示2个功能,首先添加m_closeOutCallback和m_copyrightCallback两个全局的字符串变量,这两字符串是生成客户端的javascript用的。然后在Page_Load事件添加生产这两字符内容的代码,具体代码如下:

Code
1
//关闭javascript段
2
public string m_closeOutCallback = "";
3
//版权javascript段
4
public string m_copyrightCallback = "";
5
6
protected void Page_Load(object sender, EventArgs e)
7

{
8


9
10
//通过GetCallbackEventReference生成客户端调用的javascript方法段
11
//GetCallbackEventReference (Control control,string argument,string clientCallback,string context)
12
//参数:
13
//control 处理客户端回调的服务器 Control。该控件必须实现 ICallbackEventHandler 接口并提供 RaiseCallbackEvent 方法。
14
//argument 从客户端脚本传递一个参数到服务器端的RaiseCallbackEvent 方法。
15
//clientCallback 一个客户端事件处理程序的名称,该处理程序接收服务器端事件返回的结果。
16
//context 启动回调之前在客户端的客户端脚本信息。脚本的结果传回给客户端事件处理程序。
17
m_closeOutCallback = Page.ClientScript.GetCallbackEventReference(Page, "argument", "CloseOutResponse", "context", true);
18
m_copyrightCallback = Page.ClientScript.GetCallbackEventReference(Page, "argument", "processCallbackResult", "context", true);
19


20
}
10.把上面的m_closeOutCallback和m_copyrightCallback输出到页面,切换到Default.aspx的html代码页面在页面的结尾处输入如下代码:

Code
1


2
</form>
3
4
<script language="javascript" type="text/javascript">
5
newLoad = <%=m_newLoad %>;
6
webMapAppCloseCallback = "<%=m_closeOutCallback %>";
7
webMapAppCopyrightCallback = "<%=m_copyrightCallback %>";
8
startUp();
9
10
</script>
11
</body>
12
</html>
11.新建JavaScript目录,然后在目录中新建叫WebMapApp.js的js文件,同时在Default.aspx页面用添加对
WebMapApp.js文件的引用:<script language="javascript" type="text/javascript" src="javascript/WebMapApp.js"></script> 。WebMapApp.js代码如下:

Code
1
//初始化方法
2
function startUp()
3

{
4
//暂空
5
}
6
7
//关闭页面方法
8
function CloseOut()
{
9
//向服务器端传递的参数
10
var argument = "ControlID=Map1&ControlType=Map&EventArg=CloseOutApplication";
11
var context = map.controlName;
12
//用eval执行webMapAppCloseCallback字符串,webMapAppCloseCallback = "WebForm_DoCallback('__Page',argument,CloseOutResponse,context,null,true)";
13
eval(webMapAppCloseCallback);
14
}
15
16
//在CloseOut()方法后接收服务器端事件返回的结果对页面进行响应
17
function CloseOutResponse(response, context)
{
18
window.close();
19
document.location = response;
20
}
21
22
function webMapAppGetCopyrightText()
{
23
//向服务器端传递的参数
24
var argument = "ControlID=Map1&ControlType=Map&EventArg=GetCopyrightText";
25
var context = map.controlName;
26
//用eval执行webMapAppCopyrightCallback字符串,webMapAppCopyrightCallback = "WebForm_DoCallback('__Page',argument,processCallbackResult,context,null,true)";
27
eval(webMapAppCopyrightCallback);
28
//显示CopyrightText_Panel,arcgisServer的方法
29
showFloatingPanel('CopyrightText_Panel');
30
}
31
12.上面的脚本文件中主要实现了CloseOut()和webMapAppGetCopyrightText()两客户端方法,他们是通过执行有服务器端GetCallbackEventReference生成客户端调用的javascript方法段实现无刷新的调用服务器端的方法。
13.接下来在服务器端编写响应上面这2个客户端方法的代码,这2个客户端方法都是通过WebForm_DoCallback与服务器端进行交互,所以需要在服务器端的ICallbackEventHandler.GetCallbackResult()和ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)方法中进行处理,具体代码和说明如下:

Code
1
ICallbackEventHandler 成员#region ICallbackEventHandler 成员
2
3
private string _callbackArg;
4
//负责把结果回传给客户端
5
string ICallbackEventHandler.GetCallbackResult()
6
{
7
return RaiseCallbackEvent(_callbackArg);
8
}
9
10
//负责从客户端javascript传来的参数
11
void ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)
12
{
13
_callbackArg = eventArgument;
14
}
15
16
//对客户端的请求进行处理
17
public virtual string RaiseCallbackEvent(string responseString)
18
{
19
//对请求字符串进行分割以键值的形式放到NameValueCollection m_queryString中,方便接下来的使用
20
Array keyValuePairs = responseString.Split("&".ToCharArray());
21
NameValueCollection m_queryString = new NameValueCollection();
22
string[] keyValue;
23
string response = "";
24
if (keyValuePairs.Length > 0)
25
{
26
for (int i = 0; i < keyValuePairs.Length; i++)
27
{
28
keyValue = keyValuePairs.GetValue(i).ToString().Split("=".ToCharArray());
29
m_queryString.Add(keyValue[0], keyValue[1]);
30
}
31
}
32
else
33
{
34
keyValue = responseString.Split("=".ToCharArray());
35
if (keyValue.Length > 0)
36
{
37
m_queryString.Add(keyValue[0], keyValue[1]);
38
}
39
}
40
41
//请求字符串样例:ControlID=Map1&ControlType=Map&EventArg=CloseOutApplication,这样就可以很容易理解了
42
string controlType = m_queryString["ControlType"];
43
string eventArg = m_queryString["EventArg"];
44
if (controlType == null)
45
{
46
controlType = "Map";
47
}
48
49
//根据controlType的不同对请求做不同的处理
50
switch (controlType)
51
{
52
case "Map":
53
if (eventArg == "CloseOutApplication")//关闭页面请求-CloseOut()
54
{
55
//ServerContext对象,需要 ESRI.ArcGIS.Server;
56
IServerContext context;
57
for (int i = 0; i < Session.Count; i++)
58
{
59
//清除session
60
context = Session[i] as IServerContext;
61
if (context != null)
62
{
63
context.RemoveAll();
64
context.ReleaseContext();
65
}
66
}
67
Session.RemoveAll();
68
//从webconfig中获取关闭后的页面地址
69
response = ConfigurationManager.AppSettings["CloseOutUrl"];
70
if (response == null || response.Length == 0)
71
{
72
response = "ApplicationClosed.aspx";
73
}
74
}
75
else if (eventArg == "GetCopyrightText")//显示版权-webMapAppGetCopyrightText()
76
{
77
System.Text.StringBuilder sb = new System.Text.StringBuilder();
78
//webMapAppGetCopyrightText()请求服务器后返回结果由processCallbackResult进行客户端处理
79
//关于processCallbackResult方法的参数格式 控件类型:::控件ID:::内容类型:::内容,如div:::mydiv:::content:::你好,GIS!
80
sb.AppendFormat("///:::{0}:::innercontent:::", "CopyrightTextContents");
81
int sbLength = sb.Length;
82
//获取版权内容
83
sb.Append(GetCopyrightText());
84
if (sb.Length == sbLength)
85
{
86
//没有获取,显示为没有版权
87
sb.Append("No Copyright information available.");
88
}
89
response = sb.ToString();
90
}
91
92
break;
93
default:
94
break;
95
}
96
//输出给客户端的内容
97
return response;
98
}
99
#endregion
100
101
private string GetCopyrightText()
102
{
103
System.Text.StringBuilder sb = new System.Text.StringBuilder();
104
string key = "";
105
string value = "";
106
string dataframeValue = "";
107
int layerId;
108
foreach (IMapFunctionality mapFunc in Map1.GetFunctionalities())
109
{
110
//
111
if (mapFunc.Supports("GetCopyrightText"))
112
{
113
Dictionary<string, string> crDictionary = mapFunc.GetCopyrightText();
114
System.Text.StringBuilder sb2 = new System.Text.StringBuilder();
115
string[] layerIds = null;
116
string[] layerNames = null;
117
mapFunc.GetLayers(out layerIds, out layerNames);
118
foreach (KeyValuePair<string, string> kvPair in crDictionary)
119
{
120
key = kvPair.Key;
121
value = kvPair.Value;
122
if (value != null && value.Length > 0)
123
{
124
if (key != null && key.Length > 0)
125
{
126
layerId = Convert.ToInt32(key);
127
sb2.Append(layerNames[layerId] + ": ");
128
sb2.Append(value + "<br/>");
129
}
130
else
131
dataframeValue = value;
132
}
133
}
134
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);
135
sb2.Length = 0;
136
dataframeValue = "";
137
}
138
}
139
return sb.ToString();
140
}
14.现在在页面上添加一个FloatingPanel控件id为CopyrightText_Panel,在<esri:FloatingPanel标签中添加
Style="left: 540px; position: absolute; top: 145px",Visible属性为False,在这个FloatingPanel控件添加一个div id为CopyrightTextContents.代码如下:

Code
1
<esri:FloatingPanel ID="CopyrightText_Panel" runat="server" BackColor="White" BorderColor="Gray"
2
BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana" Font-Size="8pt" ForeColor="Black"
3
Height="200px" Style="left: 491px; position: absolute; top: 387px"
4
Title="Copyright" TitleBarColor="WhiteSmoke" TitleBarHeight="20px"
5
TitleBarSeparatorLine="False" Transparency="35" Width="250px" Visible="False" Docked="False">
6
<div id="CopyrightTextContents" style="width: 100%" >Copyright Information</div>
7
</esri:FloatingPanel>
15.新建ApplicationClosed.aspx页面,等关闭地图页面后显示的页面。
16.最好在页面上添加2个btton来调用CloseOut()和webMapAppGetCopyrightText()方法,具体代码如下:

Code
1
<input id="Button1" type="button" value="关闭地图" onclick="CloseOut()" />
2
<input id="Button2" type="button" value="显示版权"onclick="webMapAppGetCopyrightText()"/>
17.最后调试运行看效果,剩下的部分下篇在写。