ArcGIS_AddDynamicData_VBNet\ChangeRenderer_ADF.aspx.vb
' Copyright 2011 ESRI ' ' All rights reserved under the copyright laws of the United States ' and applicable international laws, treaties, and conventions. ' ' You may freely redistribute and use this sample code, with or ' without modification, provided you include the original copyright ' notice and use restrictions. ' ' See the use restrictions. ' ' Illustrates how to change the renderers of existing layers in a pooled ArcGIS Server map service ' using Web ADF controls and ArcObjects. The layers are modified in a shallowly stateful way, meaning ' that the changes persist in the user's browser, but do not affect the appearance of the map service ' when viewed by other clients. Imports Microsoft.VisualBasic Imports System Public Partial Class ChangeRenderer_ADF Inherits System.Web.UI.Page #Region "Instance Variable Declarations" Private _callbackResultCollection As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResultCollection = New ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResultCollection() Private _serverObjectStateModifier As ServerObjectStateModifier = Nothing #End Region #Region "ASP.NET Page Life Cycle Event Handlers" Public Sub Page_PreInit(ByVal sender As Object, ByVal eventArgs As System.EventArgs) ' Instantiate a ServerObjectStateModifier object to use for adding and removing the dynamic layer _serverObjectStateModifier = New ServerObjectStateModifier() End Sub Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Try 'Register the Change and Reset Renderers buttons so they initiate asynchronous 'postbacks when clicked ScriptManager1.RegisterAsyncPostBackControl(ChangeRenderersButton) ScriptManager1.RegisterAsyncPostBackControl(ResetRenderersButton) ' If the renderers have been changed (indicated by the session variable), then we need to re-apply them ' so that the Web ADF components accessing the changed map resources are aware of the change. If (Not Session("RendererChanged") Is Nothing) AndAlso CBool(Session("RendererChanged")) Then ChangeRenderers(ServerObjectStateModifier.RendererAction.ApplyLast) End If AddHandler MapResourceManager1.ResourcesDispose, AddressOf MapResourceManager1_ResourcesDispose Catch exception As System.Exception ' Check whether the page is loading asynchronously. If ScriptManager1.IsInAsyncPostBack Then ' Get a callback result that will show an alert with information pertaining to the exception Dim errorCallbackResult As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult = Utility.GetErrorCallback(exception) ' Get the control that initiated the postback Dim control As System.Web.UI.Control = Utility.GetPostBackControl(Page) ' If the control is a Web ADF Control (i.e. WebCotnrol or CompositeControl), add the error ' callback to that control's callback results. Otherwise, add the error callback to the ' callback results collection member variable, which will be passed to the client in ' GetCallbackResult. If TypeOf control Is ESRI.ArcGIS.ADF.Web.UI.WebControls.WebControl Then Dim adfWebControl As ESRI.ArcGIS.ADF.Web.UI.WebControls.WebControl = TryCast(control, ESRI.ArcGIS.ADF.Web.UI.WebControls.WebControl) adfWebControl.CallbackResults.Add(errorCallbackResult) ElseIf TypeOf control Is ESRI.ArcGIS.ADF.Web.UI.WebControls.CompositeControl Then Dim adfCompositeControl As ESRI.ArcGIS.ADF.Web.UI.WebControls.CompositeControl = TryCast(control, ESRI.ArcGIS.ADF.Web.UI.WebControls.CompositeControl) adfCompositeControl.CallbackResults.Add(errorCallbackResult) Else _callbackResultCollection.Add(errorCallbackResult) ScriptManager1.RegisterDataItem(Page, _callbackResultCollection.ToString(), False) End If Else ' Since the page is in full postback, write the javascript alert code directly to the response Dim jsErrorAlert As String = String.Format("<script>{0}</script>", Utility.GetJavaScriptErrorString(exception)) Response.Write(jsErrorAlert) End If End Try End Sub #End Region #Region "Web ADF Control Event Handlers" ' If the resources' renderers have been changed (indicated by the session variable), then we need to restore them to ' their original state before the server context created as a result of the current page request is released. ' Otherwise, the altered renderers will be applied to the map service outside the scope of this request, meaning they ' will be seen by other clients using the service. Private Sub MapResourceManager1_ResourcesDispose(ByVal sender As Object, ByVal e As System.EventArgs) If (Not Session("RendererChanged") Is Nothing) AndAlso CBool(Session("RendererChanged")) Then ChangeRenderers(ServerObjectStateModifier.RendererAction.ApplyOriginal) End If End Sub #End Region #Region "ASP.NET Web Control Event Handlers" Public Sub ResetRenderersButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Try ChangeRenderers(ServerObjectStateModifier.RendererAction.ApplyOriginal) RefreshResourcesAndToc() ' Set the session variables indicating that renderers for the resources in the map are in their ' original state. We do this to share this information with the custom map handler. Session("RendererChanged") = False Catch exception As System.Exception ' Get a callback result that will alert the user of the error and add it to the callback results collection Dim errorCallbackResult As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult = Utility.GetErrorCallback(exception) _callbackResultCollection.Add(errorCallbackResult) ScriptManager1.RegisterDataItem(Page, _callbackResultCollection.ToString(), False) End Try End Sub Public Sub ChangeRenderersButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Try ChangeRenderers(ServerObjectStateModifier.RendererAction.ApplyNew) RefreshResourcesAndToc() ' Set the session variable indicating that renderers for the resources in the map have been changed. ' We do this to share this information with the custom map handler. Session("RendererChanged") = True Catch exception As System.Exception ' Get a callback result that will alert the user of the error and add it to the callback results collection Dim errorCallbackResult As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult = Utility.GetErrorCallback(exception) _callbackResultCollection.Add(errorCallbackResult) ScriptManager1.RegisterDataItem(Page, _callbackResultCollection.ToString(), False) End Try End Sub #End Region #Region "Instance Methods" ' Refreshes the Toc and all the resources in the Map Private Sub RefreshResourcesAndToc() For Each mapResourceItem As ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceItem In MapResourceManager1.ResourceItems Map1.RefreshResource(mapResourceItem.Name) Next mapResourceItem Toc1.Refresh() Map1.CallbackResults.CopyFrom(Toc1.CallbackResults) _callbackResultCollection.CopyFrom(Map1.CallbackResults) ScriptManager1.RegisterDataItem(Page, _callbackResultCollection.ToString(), False) End Sub ' Makes sure each resource in the map is initialized, then uses the ServerObjectStateModifier to apply random simple ' renderers to its layers Private Sub ChangeRenderers(ByVal rendererAction As ServerObjectStateModifier.RendererAction) For Each mapResourceItem As ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceItem In MapResourceManager1.ResourceItems If TypeOf mapResourceItem.Resource Is ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal Then _serverObjectStateModifier.ApplySimpleRenderers(TryCast(mapResourceItem.Resource, ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer.MapResourceLocal), rendererAction) End If Next mapResourceItem End Sub #End Region End Class