Common Web Mapping Application
Common_WebMappingApp_VBNet\Default.aspx.vb
' Copyright 2010 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.
' 

Imports System.Collections.Generic
Imports ESRI.ArcGIS.ADF.Web
Imports ESRI.ArcGIS.ADF.Web.UI.WebControls
Imports ESRI.ArcGIS.ADF.Web.DataSources
Imports ESRI.ArcGIS.Server
Imports ESRI.ArcGIS.ADF.ArcGISServer
Imports ESRI.ArcGIS.ADF.Web.DataSources.ArcGISServer

Namespace WebMapApp


    Partial Class WebMapApplication
        Inherits System.Web.UI.Page


        Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
            If Not Page.IsPostBack Then
                If (Map1.MapResourceManager Is Nothing Or Map1.MapResourceManager.Length = 0) Then
                    callErrorPage("No Map Resource Manager defined for the Map.", Nothing)
                End If
                If (MapResourceManager1.ResourceItems.Count = 0) Then
                    callErrorPage("The Map Resource Manager does not have a valid Resouce Item.", Nothing)
                End If
                If (Map1.PrimaryMapResourceInstance Is Nothing) Then
                    callErrorPage("The Map does not have a valid Primary Map Resource.", Nothing)
                End If
            End If
            ' Check for locale decimal delimiter and pass to client side
            Dim delimiterScript As String = "var webMapAppDecimalDelimiter = '" & GetDecimalSeparatorFromLocale() & "';"
            If (Not Page.ClientScript.IsClientScriptBlockRegistered("decimalDelimiterScript")) Then
                Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "decimalDelimiterScript", delimiterScript, True)
            End If
        End Sub 'Page_Load

        Protected Sub Page_PreRenderComplete(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.PreRenderComplete
            ' check to see if any of the resource items are non-pooled
            If Not Page.IsPostBack Then
                If TaskMenu.Items.Count > 1 Then
                    Dim i As Integer
                    For i = 0 To TaskMenu.Items.Count - 2
                        TaskMenu.Items(i).SeparatorImageUrl = "images/separator.gif"
                    Next
                End If
                CloseHyperLink.Visible = ArcGISServerLocalSupport.HasNonPooledResources(MapResourceManager1, GeocodeResourceManager1, GeoprocessingResourceManager1)

                If User.Identity.AuthenticationType = "Forms" AndAlso User.Identity.IsAuthenticated Then
                    'Set visibility using style instead of the Visible property because using the Visible property corrupts ViewState under certain circumstances 
                    LoginStatus1.Style(HtmlTextWriterStyle.Visibility) = "visible"
                    CloseHyperLink.Visible = False
                Else
                    LoginStatus1.Style(HtmlTextWriterStyle.Visibility) = "hidden"
                End If

                ' Remove the overview toggle it overviewmap doesn't exist, and identify if none of the resources support it.
                Dim ov As OverviewMap = Page.FindControl("OverviewMap1")
                Dim magnifier As Magnifier = Page.FindControl("Magnifier1")
                Dim supportsIdentify As Boolean = True
                Dim canMeasure As Boolean = True
                If Measure1.MapUnits = MapUnit.Resource_Default Then
                    canMeasure = Measure1.CanGetUnits()
                End If
                Dim tb As Toolbar = Page.FindControl("Toolbar1")
                If Not (tb Is Nothing) Then
                    Dim t As Integer
                    For t = tb.ToolbarItems.Count - 1 To 0 Step -1
                        Dim item As ToolbarItem = tb.ToolbarItems(t)
                        If item.Name = "OverviewMapToggle" And ov Is Nothing Then
                            tb.ToolbarItems.Remove(item)
                        End If
                        If item.Name = "MapIdentify" And Not supportsIdentify Then
                            tb.ToolbarItems.Remove(item)
                        End If
                        If item.Name = "Measure" And Not canMeasure Then
                            tb.ToolbarItems.Remove(item)
                        End If
                        If item.Name = "Magnifier" And magnifier Is Nothing Then
                            tb.ToolbarItems.Remove(item)
                        End If
                    Next t
                End If
            End If
        End Sub 'Page_PreRenderComplete

        Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
            ' Enforce SSL requirement.
            Dim requireSSL As Boolean
            If (Not Page.IsPostBack() And ConfigurationManager.AppSettings("RequireSSL") <> Nothing) Then
                Boolean.TryParse(ConfigurationManager.AppSettings("RequireSSL"), requireSSL)
                If (requireSSL And Not Request.IsSecureConnection) Then
                    Response.Redirect(Request.Url.ToString().Replace("http://", "https://"))
                    Return
                End If
            End If
        End Sub

        Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
            If (Request.QueryString("resetSession") = "true") Then
                ' Allows client applications (such as Manager) to pass in a query string
                ' to clear out session state for ADF controls. 
                Session.RemoveAll()
                Response.Redirect("~/default.aspx")
            End If
        End Sub

        Private Function GetDecimalSeparatorFromLocale() As Char
            Return (System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator.ToCharArray())(0)
        End Function

        '/ <summary>
        '/ Default method for catching errors that have no programmed catch point
        '/ </summary>
        Private Sub Page_Error(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Error
            Dim exception As Exception = Server.GetLastError()
            Server.ClearError()
            callErrorPage("Page_Error", exception)

        End Sub




        '/ <summary>
        '/ Common method for calling error page, passing specific parameters and messages
        '/ </summary>
        Private Sub callErrorPage(ByVal errorMessage As String, ByVal exception As Exception)
            Session("ErrorMessage") = errorMessage
            Session("Error") = exception
            Page.Response.Redirect("ErrorPage.aspx", True)
        End Sub 'callErrorPage

        Protected Sub ResourceManager_ResourcesInit(ByVal sender As Object, ByVal e As EventArgs) Handles MapResourceManager1.ResourcesInit, GeocodeResourceManager1.ResourcesInit, GeoprocessingResourceManager1.ResourcesInit, MapResourceManager2.ResourceInit
            If DesignMode Then
                Return
            End If
            Dim manager As ResourceManager = sender '
            If Not manager.FailureOnInitialize Then
                Return
            End If
            If TypeOf manager Is MapResourceManager Then
                Dim mapManager As MapResourceManager = manager '
                Dim i As Integer
                For i = 0 To mapManager.ResourceItems.Count - 1
                    Dim item As MapResourceItem = mapManager.ResourceItems(i)
                    If Not (item Is Nothing) Then
                        If item.FailedToInitialize Then
                            mapManager.ResourceItems(i) = Nothing
                        End If
                    End If
                Next i
            Else
                If TypeOf manager Is GeocodeResourceManager Then
                    Dim gcManager As GeocodeResourceManager = manager '
                    Dim i As Integer
                    For i = 0 To gcManager.ResourceItems.Count - 1
                        Dim item As GeocodeResourceItem = gcManager.ResourceItems(i)
                        If Not (item Is Nothing) Then
                            If item.FailedToInitialize Then
                                gcManager.ResourceItems(i) = Nothing
                            End If
                        End If
                    Next i
                Else
                    If TypeOf manager Is GeoprocessingResourceManager Then
                        Dim gpManager As GeoprocessingResourceManager = manager '
                        Dim i As Integer
                        For i = 0 To gpManager.ResourceItems.Count - 1
                            Dim item As GeoprocessingResourceItem = gpManager.ResourceItems(i)
                            If Not (item Is Nothing) Then
                                If item.FailedToInitialize Then
                                    gpManager.ResourceItems(i) = Nothing
                                End If
                            End If
                        Next i
                    End If
                End If
            End If
        End Sub 'ResourceManager_ResourcesInit 

        '/ <summary>
        '/ Handles call from client to clean up session.
        '/ </summary>
        <System.Web.Services.WebMethod()> Public Shared Function CleanUp() As String
            Dim cleanUpResponse As String = ConfigurationManager.AppSettings("CloseOutUrl")
            If [String].IsNullOrEmpty(cleanUpResponse) Then
                cleanUpResponse = "ApplicationClosed.aspx"
            End If
            Try
                ArcGISServerLocalSupport.ReleaseNonPooledContexts()
                HttpContext.Current.Session.RemoveAll()
                FormsAuthentication.SignOut()
            Catch
            End Try
            Return cleanUpResponse
        End Function 'CleanUp



    End Class

End Namespace