Common_AddGraphics_VBNet\App_Code\Utility.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 Microsoft.VisualBasic Imports System Public Class Utility ' Constructs a callback result that will display an error message based on the passed-in ' exception Public Shared Function GetErrorCallback(ByVal exception As System.Exception) As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult ' Create a callback result to display an error message Dim jsAlertErrorMessage As String = GetJavaScriptErrorString(exception) Dim alertCallbackResult As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult = ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult.CreateJavaScript(jsAlertErrorMessage) Return alertCallbackResult End Function ' Constructs JavaScript necessary to display an error message based on the passed-in exception. Public Shared Function GetJavaScriptErrorString(ByVal exception As System.Exception) As String ' Get the website's configuration file Dim webConfig As System.Configuration.Configuration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(System.Web.HttpContext.Current.Request.ApplicationPath) ' Get the "compilation" section of the config file Dim compilationSection As System.Web.Configuration.CompilationSection = TryCast(webConfig.GetSection("system.web/compilation"), System.Web.Configuration.CompilationSection) ' If the config file's compilation section specifies debug mode, include ' stack trace information in the error message. Otherwise, just return ' the exception message. Dim errorMessage As String = Nothing If (Not compilationSection Is Nothing) AndAlso (compilationSection.Debug) Then Dim stackTrace As String = exception.StackTrace.Replace("\", "\\") stackTrace = stackTrace.Replace(Constants.vbLf, "\n") stackTrace = stackTrace.Replace(Constants.vbCr, "\r") stackTrace = stackTrace.Replace("'", "\'") errorMessage = exception.Message.Replace("\", "\\") errorMessage = errorMessage.Replace(Constants.vbLf, "\n") errorMessage = errorMessage.Replace(Constants.vbCr, "\r") errorMessage = errorMessage.Replace("'", "\'") errorMessage = errorMessage & "\n\n" & stackTrace.Trim() Else errorMessage = exception.Message End If ' Create a callback result to display an error message Dim jsAlertException As String = "alert('" & errorMessage & "')" Return jsAlertException End Function Public Shared Function CreateErrorCallback(ByVal message As String) As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResultCollection ' Create a string containing the JavaScript necessary to display an alert with the passed-in ' message as text Dim jsAlertException As String = "alert('" & message & "')" ' Create a callback result encapsulating the JavaScript Dim alertCallbackResult As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult = ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResult.CreateJavaScript(jsAlertException) ' Create a Web ADF CallbackResultCollection and add the callback results created above to it Dim callbackResultCollection As ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResultCollection = New ESRI.ArcGIS.ADF.Web.UI.WebControls.CallbackResultCollection() callbackResultCollection.Add(alertCallbackResult) Return callbackResultCollection End Function Friend Shared Function FindControl(ByVal controlID As String, ByVal page As System.Web.UI.Page) As System.Web.UI.Control ' Make sure the passed-in page and control are not null If page Is Nothing OrElse controlID Is Nothing Then Return Nothing End If ' Try getting a reference to the control being sought via the page's FindControl function Dim webControl As System.Web.UI.Control = page.FindControl(controlID) ' Check whether the control was found If webControl Is Nothing Then ' Call method to traverse the Page's controls and find the unique id of the control ' having the passed in control ID Dim uniqueControlID As String = GetControlUniqueID(controlID, page.Controls) If Not uniqueControlID Is Nothing Then webControl = page.FindControl(uniqueControlID) Else webControl = page.FindControl(controlID) End If End If Return webControl End Function Friend Shared Function GetControlUniqueID(ByVal controlID As String, ByVal controls As System.Web.UI.ControlCollection) As String ' Declare a Control object to store references to controls in the passed-in ControlCollection Dim control As System.Web.UI.Control ' Declare a string variable to store references to the UniqueIDs of controls in the passed-in ' collection Dim uniqueID As String = Nothing ' Iterate through the controls in the passed-in collection Dim i As Integer = 0 Do While i < controls.Count ' Get a reference to the current control control = controls(i) ' Check whether the current control's ID matches the passed in control ID If control.ID = controlID Then ' The control's ID matches, so get a reference to its UniqueID and exit the loop uniqueID = control.UniqueID Exit Do End If ' Check whether the current control contains any child controls If control.Controls.Count > 0 Then ' Recursively call GetControlUniqueID with the passed-in control ID and the current ' control's collection of child controls uniqueID = GetControlUniqueID(controlID, control.Controls) ' Check whether the ID was found. If so, exit the loop If Not uniqueID Is Nothing Then Exit Do End If End If i += 1 Loop Return uniqueID End Function Friend Shared Function RenderControlHtml(ByVal webControl As System.Web.UI.Control) As String ' Instantiate StringWriter and HtmlTextWriter objects to render the control to and ' retrieve the control's HTML Dim stringWriter As System.IO.StringWriter = New System.IO.StringWriter() Dim htmlTextWriter As System.Web.UI.HtmlTextWriter = New System.Web.UI.HtmlTextWriter(stringWriter) ' Render the passed-in control to the HtmlTextWriter webControl.RenderControl(htmlTextWriter) ' Retrieve the control's html as a string from the StringWriter. The state ' of the StringWriter is automatically updated via its reference to the HtmlTextWriter Dim htmlContent As String = stringWriter.ToString() ' Since we are done with the StringWriter, close it stringWriter.Close() ' Return the control's HTML as a string Return htmlContent End Function End Class