Common_QueryNewWindow_CSharp\Default.aspx.cs
// 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. // public partial class Default : System.Web.UI.Page { protected void btnQuery_Click(object sender, System.EventArgs eventArgs) { try { // Specify the name of the layer to query string queryLayerName = "States"; // Specify the name of the field to query string queryFieldName = "STATE_NAME"; // Specify the value sought. With the out-of-the-box implementation, the query will search // for values in the query field that begin with this value string matchValue = "A"; // Since no Map is present, we need to initialize the MapResourceManager if (!MapResourceManager1.Initialized) MapResourceManager1.Initialize(); // Retrieve the resource to be used ESRI.ArcGIS.ADF.Web.UI.WebControls.MapResourceItem mapResourceItem = MapResourceManager1.ResourceItems.Find("Map Resource"); ESRI.ArcGIS.ADF.Web.DataSources.IGISResource gisResource = mapResourceItem.Resource; // We also need to initialize the resource underlying the resource item we will use if (!gisResource.Initialized) mapResourceItem.InitializeResource(); // Generate a random number. This will be used to enable storing and retrieving multiple // tables to and from Session System.Random random = new System.Random(); int randomTableID = random.Next(0, 1000); // Create a Common Data Source API QueryFunctionality object to use for querying the resource ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality commonQueryFunctionality = (ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality)gisResource.CreateFunctionality( typeof(ESRI.ArcGIS.ADF.Web.DataSources.IQueryFunctionality), null); // Populate arrays with the IDs and names of the queryable layers contained in the resource string[] layerIDs; string[] layerNames; commonQueryFunctionality.GetQueryableLayers(null, out layerIDs, out layerNames); // Find the index of the query layer in the layer ID and name arrays int queryLayerIndex = 0; for (int i = 0; i < layerNames.Length; i++) { if (layerNames[i] == queryLayerName) { queryLayerIndex = i; break; } } // Initialize a query filter to search the specified field for any values that begin with // the specified value. ESRI.ArcGIS.ADF.Web.QueryFilter adfQueryFilter = new ESRI.ArcGIS.ADF.Web.QueryFilter(); ESRI.ArcGIS.ADF.StringCollection fieldStringCollection = new ESRI.ArcGIS.ADF.StringCollection(queryFieldName, ','); adfQueryFilter.SubFields = fieldStringCollection; adfQueryFilter.ReturnADFGeometries = false; adfQueryFilter.WhereClause = string.Format("{0} LIKE '{1}%'", queryFieldName, matchValue); // Execute the query and store the results in a DataTable System.Data.DataTable resultsDataTable = commonQueryFunctionality.Query(null, layerIDs[queryLayerIndex], adfQueryFilter); // Retrieve the LayerFormat object for the query layer. This will provide access to field aliases. ESRI.ArcGIS.ADF.Web.UI.WebControls.LayerFormat layerFormat = ESRI.ArcGIS.ADF.Web.UI.WebControls.LayerFormat.FromMapResourceManager(MapResourceManager1, "Map Resource", layerIDs[queryLayerIndex]); // Set the name of the query field column in the results data table to the query field alias for (int i = 0; i < layerFormat.Fields.Count; i++) { // Before changing the column name, make sure that the query field has a valid alias defined // that is distinct from the actual field name if ((layerFormat.Fields[i].Name == queryFieldName) && (layerFormat.Fields[i].Alias != queryFieldName) && (layerFormat.Fields[i].Alias != "")) { resultsDataTable.Columns[queryFieldName].ColumnName = layerFormat.Fields[i].Alias; } } // Store the results data table in Session, using the random table ID as part of the key string tableSessionKey = string.Format("dataTable{0}", randomTableID); Session[tableSessionKey] = resultsDataTable; // Construct a JavaScript string to inject into the page that will open the TableDialog page. // Include the random table ID in this page so that (a) TableDialog can retrieve the proper table // from Session and (b) a new window is always opened, even when the query is executed multiple // times string jsOpenQueryResultsWindow = string.Format("<script>window.open('TableDialog.aspx?id={0}" + "', '{0}', 'dependent=yes ,width=400, height=200, status=no, toolbar=no, menubar=no, " + "location=no, resizable=yes, scrollbars=yes'); </script>", randomTableID); Response.Write(jsOpenQueryResultsWindow); // Construct a JavaScript string to open the query results in a modeless dialog. For more info, see: // http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/methods/showmodelessdialog.asp //string jsOpenQueryResultsModelessDialog = string.Format("<script>window.showModelessDialog(" + // "'TableDialog.aspx?id={0}'); </script>", randomTableID); //Response.Write(jsOpenQueryResultsModelessDialog); } catch (System.Exception exception) { string jsAlertError = string.Format("<script>{0}</script>", GetJavaScriptErrorString(exception)); Response.Write(jsAlertError); } } // Constructs JavaScript necessary to display an error message based on the passed-in exception. private string GetJavaScriptErrorString(System.Exception exception) { // Get the website's configuration file System.Configuration.Configuration webConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration( System.Web.HttpContext.Current.Request.ApplicationPath); // Get the "compilation" section of the config file System.Web.Configuration.CompilationSection compilationSection = webConfig.GetSection("system.web/compilation") as 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. string errorMessage = null; if ((compilationSection != null) && (compilationSection.Debug)) { string stackTrace = exception.StackTrace.Replace("\\", "\\\\"); errorMessage = exception.Message + "\\n\\n" + stackTrace.Trim(); } else errorMessage = exception.Message; // Create a callback result to display an error message string jsAlertException = "alert('" + errorMessage + "')"; return jsAlertException; } }